dot big bang

Developer API
Menu

Class Random

All
  • Public
  • Public/Protected
  • All

Hierarchy

  • Random

Index

Constructors

constructor

  • new Random(seed?: string | number): Random
  • Constructs a seedable random value generator.

    Parameters

    • Optional seed: string | number

      If provided, this seed will be used to determine the sequence of numbers this RNG produces. You can re-use a seed in the future to get the same sequence of values, which may be useful for world map generation, etc. If unspecified, a random seed will be used.

    Returns Random

Properties

offset

offset: number

The current offset; whenever a value is requested from the RNG, this offset increases by one. If you change the offset, values from a different part of the sequence of random numbers can be retrieved.

Static hashStringToNum

hashStringToNum: (input: string, seed?: number) => number

Type declaration

    • (input: string, seed?: number): number
    • Hash a string into an integer suitable for use as a seed, using the cyrb53 algorithm.

      Parameters

      • input: string

        The string to hash.

      • Optional seed: number

        An optional seed value that affects the output. If omitted, 0 will be used.

      Returns number

      A positive integer.

Static makeSeed

makeSeed: () => number

Type declaration

    • (): number
    • Generates a random positive integer suitable for use as a seed.

      Returns number

Methods

chance

  • chance(fraction: number): boolean
  • Returns a random boolean based on the specified chance value.

    Parameters

    • fraction: number

      Determines how often the boolean will be true. For instance, chance(1/6) will return true 1/6th of the time.

    Returns boolean

    true or false.

    Example:

    const rng = new Random();
    const result = rng.chance(1/10); // result is true one tenth of the time
    

    This method advances this RNG's offset in the sequence by 1.

flipCoin

  • flipCoin(): boolean
  • Returns a random boolean. There is an equal chance whether it will be true or false (50/50).

    This method advances this RNG's offset in the sequence by 1.

    Returns boolean

index

  • index(length: number): number
  • Given the length of an Array-like structure, returns an integer that could be used as an index in that structure.

    Parameters

    • length: number

      The length of the Array (or string, or whatever).

    Returns number

    A randomly-chosen index; ie. an integer in the range [0, length - 1].

    This method advances this RNG's offset in the sequence by 1.

numberInRange

  • numberInRange(lower: number, upper: number): number
  • Get a random number within the specified range.

    Parameters

    • lower: number

      The lower bound of the range.

    • upper: number

      The upper bound of the range.

    Returns number

    The resulting random number, in the range [lower, upper). The number may (and most likely will) have a fractional part.

    This method advances this RNG's offset in the sequence by 1.

reseed

  • reseed(seed?: string | number): void
  • Changes the seed for this RNG to a different value.

    Parameters

    • Optional seed: string | number

      The new seed.

      If no seed argument is provided, a random seed will be chosen.

      This method does not affect this RNG's offset. To reset the offset, call .reset().

    Returns void

reset

  • reset(): void
  • Resets this RNG so that it starts giving values from the beginning of the sequence again.

    This method sets this RNG's offset to 0.

    Returns void

roll

  • roll(dieSides: number): number
  • Rolls a virtual die and returns the result.

    Parameters

    • dieSides: number

      The number of sides the die should have. For example, 6.

    Returns number

    The result of the die roll; an integer in the range [1, dieSides].

    Example:

    const rng = new Random();
    const result = rng.roll(6); // rolls a d6
    

    This method advances this RNG's offset in the sequence by 1.

shuffle

  • shuffle(array: any[]): void
  • Shuffle the contents of an Array in-place, mutating the array.

    This method advances this RNG's offset in the sequence by an amount equivalent to the array's length.

    Parameters

    • array: any[]

      The array to shuffle.

      Internally, this method uses the Fisher-Yates shuffling algorithm.

      If you would like to shuffle a clone of the array instead of mutating it in-place, you can do this:

      const myClone = [...myArray];
      Random.shuffle(myClone);
      

    Returns void

string

  • string(length: number, charsToPickFrom?: string[]): string
  • Generates a string of the provided length containing random characters.

    Parameters

    • length: number

      The length of the string to generate.

    • Optional charsToPickFrom: string[]

      An array containing characters that could end up in the result. If unspecified, defaults to an array containing a through z, A through Z, and 0 through 9.

    Returns string

    The resulting randomly-generated string.

    This method advances this RNG's offset in the sequence by an amount equivalent to the length parameter.

weightedSample

  • weightedSample<ValueType>(weightsAndValues: { value: ValueType; weight: number }[]): ValueType
  • Using the specified weights for the given values, pick a random value.

    Type parameters

    • ValueType

    Parameters

    • weightsAndValues: { value: ValueType; weight: number }[]

      An array of objects shaped like { weight: number, value: any }.

      Example:

      const random = new Random();
      const result = random.weightedSample([
        { value: "apple", weight: 1 },
        { value: "potato", weight: 3 },
        { value "banana", weight: 0.5 }
      ]);
      
      // potato will be picked more commonly than apple, and banana will be
      // the rarest.
      console.log(result); // either "apple", "potato", or "banana".
      

      You can use any type for the value properties; as such, you can get numbers, strings, objects... etc.

      This method advances this RNG's offset in the sequence by one.

    Returns ValueType

Static chance

  • chance(fraction: number): boolean
  • Returns a random boolean based on the specified chance value.

    Parameters

    • fraction: number

      Determines how often the boolean will be true. For instance, chance(1/6) will return true 1/6th of the time.

    Returns boolean

    true or false.

    Example:

    const result = Random.chance(1/10); // result is true one tenth of the time
    

Static flipCoin

  • flipCoin(): boolean
  • Returns a random boolean. There is an equal chance whether it will be true or false (50/50).

    Returns boolean

Static index

  • index(length: number): number
  • Given the length of an Array-like structure, returns an integer that could be used as an index in that structure.

    Parameters

    • length: number

      The length of the Array (or string, or whatever).

    Returns number

    A randomly-chosen index; ie. an integer in the range [0, length - 1].

Static numberInRange

  • numberInRange(lower: number, upper: number): number
  • Get a random number within the specified range.

    Parameters

    • lower: number

      The lower bound of the range.

    • upper: number

      The upper bound of the range.

    Returns number

    The resulting random number, in the range [lower, upper). The number may (and most likely will) have a fractional part.

Static roll

  • roll(dieSides: number): number
  • Rolls a virtual die and returns the result.

    Parameters

    • dieSides: number

      The number of sides the die should have. For example, 6.

    Returns number

    The result of the die roll; an integer in the range [1, dieSides].

    Example:

    const result = Random.roll(6); // rolls a d6
    

Static shuffle

  • shuffle(array: any[]): void
  • Shuffle the contents of an Array in-place, mutating the array.

    Parameters

    • array: any[]

      The array to shuffle.

      Internally, this method uses the Fisher-Yates shuffling algorithm.

      If you would like to shuffle a clone of the array instead of mutating it in-place, you can do this:

      const myClone = [...myArray];
      Random.shuffle(myClone);
      

    Returns void

Static string

  • string(length: number, charsToPickFrom?: string[]): string
  • Generates a string of the provided length containing random characters.

    Parameters

    • length: number

      The length of the string to generate.

    • Optional charsToPickFrom: string[]

      An array containing characters that could end up in the result. If unspecified, defaults to an array containing a through z, A through Z, and 0 through 9.

    Returns string

    The resulting randomly-generated string.

Static weightedSample

  • weightedSample<ValueType>(weightsAndValues: { value: ValueType; weight: number }[]): ValueType
  • Using the specified weights for the given values, pick a random value.

    Type parameters

    • ValueType

    Parameters

    • weightsAndValues: { value: ValueType; weight: number }[]

      An array of objects shaped like { weight: number, value: any }.

      Example:

      const result = Random.weightedSample([
        { value: "apple", weight: 1 },
        { value: "potato", weight: 3 },
        { value "banana", weight: 0.5 }
      ]);
      
      // potato will be picked more commonly than apple, and banana will be
      // the rarest.
      console.log(result); // either "apple", "potato", or "banana".
      

      You can use any type for the value properties; as such, you can get numbers, strings, objects... etc.

      This method advances this RNG's offset in the sequence by one.

    Returns ValueType