當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript random-js.bool函數代碼示例

本文整理匯總了TypeScript中random-js.bool函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript bool函數的具體用法?TypeScript bool怎麽用?TypeScript bool使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了bool函數的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: mutateNode

function mutateNode(keys: string[], random: Random, entity: Entity): Entity {
  const willMutate = random.bool(MUTATE_PERCENTAGE);

  if (!willMutate) {
    return entity;
  }

  const mutationId = random.integer(MIN_SEED, MAX_SEED);
  const mutation = chooseMutation(keys, mutationId, entity);

  return applyMutation(entity, mutation);
}
開發者ID:helix-pi,項目名稱:helix-pi,代碼行數:12,代碼來源:index.ts

示例2: percent

 percent(p: number) {
     return randomjs.bool(p)(this.engine);
 }
開發者ID:sbj42,項目名稱:sbj42.github.io,代碼行數:3,代碼來源:random.ts

示例3: bool

export function bool(numerator?: number, denominator?: number): boolean {
    // any below because of bad TS definitions in the @types package.
    return Random.bool(numerator as any, denominator as any)(engine);
}
開發者ID:MPennanti,項目名稱:HungryDragon,代碼行數:4,代碼來源:random.ts

示例4: generateEntity

export function generateEntity(
  seed: number,
  keys: string[],
  depth = 0
): Entity {
  const random = new Random(Random.engines.mt19937().seed(seed));

  const isLeaf = depth > 1 || random.bool();

  if (isLeaf) {
    const possibleCommands = [
      "move",
      "setVelocity",
      "multiplyVelocity",
      "noop"
    ];

    const command = random.pick(possibleCommands);

    if (command === "noop") {
      return {
        type: command,
        id: seed.toString()
      };
    }

    if (command === "move") {
      const direction = random.pick([
        "up",
        "right",
        "left",
        "down"
      ]) as Direction;
      const amount = random.integer(0, 20) / 10;

      return {
        type: command,
        id: seed.toString(),
        direction,
        amount
      };
    }

    if (command === "setVelocity") {
      let velocity;

      if (random.bool(0.5)) {
        // cardinal direction
        const scalar = random.integer(0, 20) / 20;

        velocity = random.pick([
          { x: 0, y: -scalar },
          { x: scalar, y: 0 },
          { x: -scalar, y: 0 },
          { x: 0, y: scalar }
        ]);
      } else {
        velocity = {
          x: random.integer(-20, 20) / 20,
          y: random.integer(-20, 20) / 20
        };
      }

      return {
        type: command,
        id: seed.toString(),
        velocity
      };
    }

    if (command === "multiplyVelocity") {
      const scalar = random.integer(-20, 20) / 20;

      return {
        type: command,
        id: seed.toString(),
        scalar
      };
    }

    throw new Error("Unimplemented command");
  } else {
    const possibleCommands = [
      "sequence",
      "inputConditional",
      "collisionConditional"
    ];

    if (depth === 0) {
      possibleCommands.push("onCreate");
    }
    // TODO - fix definitely typed definition
    const command = (random as any).pick(possibleCommands) as any;

    if (command === "collisionConditional") {
      return {
        type: command,
        id: seed.toString(),
        children: makeChildren(random, 1, keys, depth)
      };
//.........這裏部分代碼省略.........
開發者ID:helix-pi,項目名稱:helix-pi,代碼行數:101,代碼來源:index.ts

示例5: chooseMutation

function chooseMutation(
  keys: string[],
  seed: number,
  entity: Entity
): Mutation {
  const random = new Random(Random.engines.mt19937().seed(seed));

  if (random.bool(0.5)) {
    return {
      id: seed.toString(),
      type: "ConvertToNoopMutation"
    };
  }

  if (entity.type === "sequence") {
    const types = [
      "NewEntityMutation",
      "RemovalMutation",
      "SwitchMutation",
      "ReplaceMutation"
    ];

    const mutationType = random.pick(types);

    if (mutationType === "NewEntityMutation") {
      const newEntity = generateEntity(
        random.integer(MIN_SEED, MAX_SEED),
        keys,
        0
      );

      return {
        id: seed.toString(),
        type: mutationType,
        newEntity,
        spliceIndex: random.integer(0, (entity as Branch).children.length)
      };
    }

    if (mutationType === "ReplaceMutation") {
      const newEntity = generateEntity(
        random.integer(MIN_SEED, MAX_SEED),
        keys,
        0
      );

      return {
        id: seed.toString(),
        type: mutationType,
        newEntity,
        spliceIndex: random.integer(0, (entity as Branch).children.length - 1)
      };
    }

    if (mutationType === "RemovalMutation") {
      return {
        id: seed.toString(),
        type: mutationType,
        removeIndex: random.integer(0, entity.children.length - 1)
      };
    }

    if (mutationType === "SwitchMutation") {
      return {
        id: seed.toString(),
        type: mutationType,
        fromIndex: random.integer(0, entity.children.length - 1),
        toIndex: random.integer(0, entity.children.length - 1)
      };
    }
  }

  if (entity.type === "onCreate") {
    const newEntity = generateEntity(
      random.integer(MIN_SEED, MAX_SEED),
      keys,
      0
    );

    return {
      id: seed.toString(),
      type: "ReplaceMutation",
      newEntity,
      spliceIndex: random.pick([0, 1])
    };
  }

  if (entity.type === "collisionConditional") {
    const newEntity = generateEntity(
      random.integer(MIN_SEED, MAX_SEED),
      keys,
      0
    );

    return {
      id: seed.toString(),
      type: "ReplaceMutation",
      newEntity,
      spliceIndex: random.pick([0, 1])
    };
//.........這裏部分代碼省略.........
開發者ID:helix-pi,項目名稱:helix-pi,代碼行數:101,代碼來源:index.ts


注:本文中的random-js.bool函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。