Generates a random boolean.
Return a boolean based on a probability.
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.
Selects a random element from array
.
Generates a floating point number between min
(inclusive) and max
(exclusive).
Generates an integer between min
(inclusive) and max
(exclusive).
Selects a random element from items
.
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.
A number between 0-1
Shuffles array
in place with Fisher-Yates.
This function modifies array
. Use shuffled to return a new
shuffled array instead.
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" },
])
Generated using TypeDoc
Pseudo Random Number Generator
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.