LittleJS 1.31.0

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:616

Create a random number generator with the seed passed in

seed number optional, default 123456789

Starting seed, a whole number, 0 as an integer uses the default seed; a fraction is cut to a whole number, so Math.random() is always 0, the default, use randInt(1e9) for a seed of chance

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:626

random seed, set it to reseed

Methods

angle() → numberengineMath.js:678

Returns a random angle between -PI and PI

bool(chance) → booleanengineMath.js:657

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

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

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:633

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:667

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:652

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:726

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:697

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:711

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:661

Randomly returns either -1 or 1 deterministically

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

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:704

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