LittleJS 1.30.1

Engine

RandomGenerator

Seeded random number generator

  • Can be used to create a deterministic random number sequence
  • The seed works as a 32 bit integer, and one that is 0 as an integer (0, a fraction between -1 and 1, or a multiple of 2**32) uses the default seed

Constructor

new RandomGenerator(seed)engineMath.js:608

Create a random number generator with the seed passed in

seed number optional, default 123456789

Starting seed, 0 as an integer uses the default seed

let r = new RandomGenerator(123); // random number generator with seed 123
let a = r.float();                // random value between 0 and 1
let b = r.int(10);                // random integer between 0 and 9
r.seed = 123;                     // reset the seed
let c = r.float();                // the same value as a

Members

Methods

Members

seed : numberengineMath.js:615

random seed, set it to reseed

Methods

angle() → numberengineMath.js:667

Returns a random angle between -PI and PI

bool(chance) → booleanengineMath.js:646

Randomly returns true or false given the chance of true passed in

chance number optional, default 0.5
direction(length) → Vector2engineMath.js:680

Returns a seeded Vector2 pointing a random way with the length passed in, the twin of randVec2

length number optional, default 1
float(valueA, valueB) → numberengineMath.js:622

Returns a seeded random value between the two values passed in

valueA number optional, default 1
valueB number optional, default 0
floatSign(valueA, valueB) → numberengineMath.js:656

Returns a seeded random value between the two values passed in with a random sign

valueA number optional, default 1
valueB number optional, default 0
int(valueA, valueB) → numberengineMath.js:641

Returns a floored seeded random value between the two values passed in

valueA number
valueB number optional, default 0
mutateColor(color, amount, alphaAmount) → ColorengineMath.js:715

Returns a new color that has each component randomly adjusted

color Color
amount number optional, default 0.05
alphaAmount number optional, default 0
pick(items) → T|undefinedengineMath.js:686

Returns a seeded random element of an array or a Set, the twin of randPick

items Array<T>|Set<T>
randColor(colorA, colorB, linear) → ColorengineMath.js:700

Returns a random color between the two passed in colors, combine components if linear

colorA Color optional, default WHITE
colorB Color optional, default BLACK
linear boolean optional, default false
sign() → numberengineMath.js:650

Randomly returns either -1 or 1 deterministically

vec2(valueA, valueB) → Vector2engineMath.js:674

Returns a seeded vec2 with each component between the two values passed in

  • A point in a square, not a random direction like randVec2, see direction for that
valueA number optional, default 1
valueB number optional, default 0
weighted(items, weights) → T|undefinedengineMath.js:693

Returns a seeded random element picked in proportion to its weight, the twin of randWeighted

items Array<T>|Set<T>
weights Array<number>

One for each item in order, 0 or more