LittleJS 1.28.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:560

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

random seed, set it to reseed

Methods

angle() → numberengineMath.js:619

Returns a random angle between -PI and PI

bool(chance) → booleanengineMath.js:598

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

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

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

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

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

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

Returns a new color that has each component randomly adjusted

color Color
amount number optional, default 0.05
alphaAmount number optional, default 0
randColor(colorA, colorB, linear) → ColorengineMath.js:639

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

Randomly returns either -1 or 1 deterministically

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

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