Class Random

Helper with methods to generate random numbers.

Example

Use the global shared instance:

let value = bge.random.int(10, 20);

Create an instance with a seed string:

let myRandom = new bge.Random("my cool seed");
let value = myRandom.int(10, 20);

Hierarchy

  • Random

Constructors

Accessors

Methods

Constructors

  • Create an instance of Random with a given seed. Instances created with the same seed will output the same sequence of values, as long as each method is called in the same order each time.

    Parameters

    • seed: string

      String to be hashed and used as an initial state.

    Returns Random

    Example

    Create an instance with a seed string:

    let myRandom = new bge.Random("my cool seed");
    let value = myRandom.int(10, 20);

Accessors

  • get seed(): string
  • Seed string used when creating this instance.

    Returns string

    Example

    Print the seed of the shared instance:

    console.log(bge.random.seed);
    

Methods

  • Returns true with the given probability, which is 0.5 by default (50%).

    Parameters

    • probability: number = 0.5

      Chance of returning true, between 0 and 1.

    Returns boolean

    Example

    Fair coin flip:

    console.log(bge.random.chance() ? "Heads" : "Tails");
    

    Weighted coin flip, with a 75% bias towards heads:

    console.log(bge.random.chance(0.75) ? "Heads" : "Tails");
    
  • Generates a uniformly-distributed floating-point number between 0 (inclusive) and 1 (exclusive).

    Returns number

    Example

    Generate a floating-point value between 0 and 1:

    let value = bge.random.float();
    
  • Generates a uniformly-distributed floating-point number between 0 (inclusive) and max (exclusive).

    Parameters

    • max: number

      The generated number will be less than this value.

    Returns number

    Example

    Generate a floating-point value between 0 and 10:

    let value = bge.random.float(10);
    
  • Generates a uniformly-distributed floating-point number between min (inclusive) and max (exclusive).

    Parameters

    • min: number

      The generated number will be at least this value.

    • max: number

      The generated number will be less than this value.

    Returns number

    Example

    Generate a floating-point value between 10 and 20:

    let value = bge.random.float(10, 20);
    
  • Generates a uniformly-distributed integer between 0 (inclusive) and max (exclusive).

    Parameters

    • max: number

      The generated number will be less than this value.

    Returns number

    Example

    Generate either 0, 1, 2, 3 or 4:

    let value = bge.random.int(5);
    
  • Generates a uniformly-distributed integer between min (inclusive) and max (exclusive).

    Parameters

    • min: number

      The generated number will be at least this value.

    • max: number

      The generated number will be less than this value.

    Returns number

    Example

    Generate either 10, 11, 12, 13 or 14:

    let value = bge.random.int(10, 15);
    
  • Returns a uniformly selected random item from an array.

    Type Parameters

    • TItem

    Parameters

    • items: readonly TItem[]

      Array of items to select from.

    Returns TItem

    An item chosen from the given array.

    Example

    Pick a random name from a list:

    let list = ["Alice", "Bob", "Charlie"];
    let name = bge.random.item(list);
  • Shuffles all items of the given array in-place.

    Type Parameters

    • TItem

    Parameters

    • array: TItem[]

      Array of items to shuffle

    Returns void

    Example

    Shuffle a list of numbers:

    let list = [1, 2, 3, 4, 5];
    bge.random.shuffle(list);
  • Shuffles a range of items of the given array in-place.

    Type Parameters

    • TItem

    Parameters

    • array: TItem[]

      Array of items to shuffle.

    • from: number

      Start of the range to shuffle, from 0.

    • to: number

      Exclusive end of the range to shuffle.

    Returns void

    Example

    Shuffle the middle 3 items of a list with 5 numbers:

    let list = [1, 2, 3, 4, 5];
    bge.random.shuffle(list, 1, 4);

Generated using TypeDoc