Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "prng"

Utilities for generating deterministic sequences of random numbers.

import { PRNG } from "silmarils";
// or
import * as PRNG from "silmarils/prng";

// seed a pseudo random number generator
let gen = PRNG.generator(123);

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

Note: If you don't need multiple generators, then it's simpler to use silmarils/rng instead.

Index

Type aliases

WeightedItem

WeightedItem<T>: { value: T; weight: number }

Type parameters

  • T

Type declaration

  • value: T
  • weight: number

Functions

boolean

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

    Parameters

    Returns boolean

chance

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

    Parameters

    • rng: RNG
    • 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

clone

  • Creates a copy of rng.

    Parameters

    Returns RNG

element

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

    Type parameters

    • Item

    Parameters

    • rng: RNG
    • array: keyof Item[]

    Returns Item

float

  • float(rng: RNG, min?: number, max?: number): number
  • Generates a floating point number between min (inclusive) and max (exclusive).

    Parameters

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

    Returns number

generator

  • generator(seed: number): RNG
  • Create a new RNG from a seed.

    Parameters

    • seed: number

    Returns RNG

int

  • int(rng: RNG, min?: number, max?: number): number
  • Generates an integer between min (inclusive) and max (exclusive).

    Parameters

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

    Returns number

item

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

    Type parameters

    • Item

    Parameters

    • rng: RNG
    • Rest ...items: Item[]

    Returns Item

next

  • next(rng: RNG): number
  • Generate the next number and update the state of rng.

    Note: This function is a building block for the other functions in this module and you should generally use those instead.

    Parameters

    Returns number

    A number between 0-1

shuffle

  • shuffle(rng: RNG, array: any[]): void
  • Shuffles array in place with Fisher-Yates.

    This function modifies array. Use shuffled to return a new shuffled array instead.

    Parameters

    • rng: RNG
    • array: any[]

    Returns void

shuffled

  • shuffled<T>(rng: RNG, array: T[]): T[]
  • Creates a shuffled version of an array.

    This function returns a new array. Use shuffle to modify array in place instead.

    Type parameters

    • T

    Parameters

    • rng: RNG
    • array: T[]

    Returns T[]

weighted

  • Select a value from a list of weighted items. A higher weight means an item is more likely to be selected.

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

    Type parameters

    • T

    Parameters

    Returns T

Generated using TypeDoc