Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "rng"

import { RNG } from "silmarils";
// or
import * as RNG from "silmarils/rng";

RNG.int(0, 10)          // random int between 0 and 10
RNG.float(-1.0, 1.0)    // random float between -1 and 1
RNG.boolean()           // random boolean
RNG.element([1, 2, 3])  // random element from array
RNG.item(1, 2, 3)       // random argument
RNG.shuffle([1, 2, 3])  // shuffle in place
RNG.shuffled([1, 2, 3]) // return a shuffled copy

Index

Variables

Let gen

gen: RNG = PRNG.generator(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER))

The generator that this module uses internally. It can be re-seeded with the seed function.

Functions

boolean

  • boolean(): boolean
  • Generates a random boolean.

    Returns boolean

chance

  • chance(probability: number): boolean
  • Return a boolean based on a probability.

    Parameters

    • probability: number

      A number between 0 and 1 representing the likelihood that true will be returned. A probability of 0 will always return false and a probability of 1 will always return true.

    Returns boolean

element

  • element<Item>(array: keyof Item[]): Item
  • Selects a random element from array.

    Type parameters

    • Item

    Parameters

    • array: keyof Item[]

    Returns Item

float

  • float(min?: number, max?: number): number
  • Generates a random float between min (inclusive) and max (exlusive).

    Parameters

    • Default value min: number = 0
    • Default value max: number = Number.MAX_VALUE

    Returns number

int

  • int(min?: number, max?: number): number
  • Generates a random integer between min (inclusive) and max (exlusive).

    Parameters

    • Default value min: number = 0
    • Default value max: number = Number.MAX_SAFE_INTEGER

    Returns number

item

  • item<Item>(...items: Item[]): Item
  • Selects a random element from items.

    Type parameters

    • Item

    Parameters

    • Rest ...items: Item[]

    Returns Item

next

  • next(): number
  • Generates the next random number. Comparable to Math.random.

    Returns number

    A number between 0-1

seed

  • seed(seed: number): void
  • Sets the seed for the internal random number generator.

    Parameters

    • seed: number

    Returns void

shuffle

  • shuffle(array: any[]): void
  • Shuffles an array in place.

    If you want to return a new array, use shuffled.

    Parameters

    • array: any[]

    Returns void

shuffled

  • shuffled<T>(array: T[]): T[]
  • Creates a shuffled copy of an array.

    If you want to shuffle the array in place, use shuffle.

    Type parameters

    • T

    Parameters

    • array: T[]

    Returns T[]

weighted

  • weighted<T>(items: PRNG.WeightedItem<T>[]): T
  • Select a value from a list of weighted items. A higher weight means an item is more likely to be selected.

    let item = RNG.weighted([
      { weight: 1, value: "foo" },
      { weight: 2, value: "bar" },
      { weight: 3, value: "baz" },
    ])

    Type parameters

    • T

    Parameters

    • items: PRNG.WeightedItem<T>[]

    Returns T

Generated using TypeDoc