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(seedopt)

Create a random number generator with the seed passed in

Parameters:
NameTypeAttributesDefaultDescription
seednumber<optional>
123456789

Starting seed, 0 as an integer uses the default seed

Example
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

seed

Properties
TypeDescription
number

random seed, set it to reseed

Methods

angle() → {number}

Returns a random angle between -PI and PI

Returns:
Type: 
number

bool(chanceopt) → {boolean}

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

Parameters:
NameTypeAttributesDefaultDescription
chancenumber<optional>
0.5
Returns:
Type: 
boolean

direction(lengthopt) → {Vector2}

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

Parameters:
NameTypeAttributesDefaultDescription
lengthnumber<optional>
1
Returns:
Type: 
Vector2

float(valueAopt, valueBopt) → {number}

Returns a seeded random value between the two values passed in

Parameters:
NameTypeAttributesDefaultDescription
valueAnumber<optional>
1
valueBnumber<optional>
0
Returns:
Type: 
number

floatSign(valueAopt, valueBopt) → {number}

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

Parameters:
NameTypeAttributesDefaultDescription
valueAnumber<optional>
1
valueBnumber<optional>
0
Returns:
Type: 
number

int(valueA, valueBopt) → {number}

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

Parameters:
NameTypeAttributesDefaultDescription
valueAnumber
valueBnumber<optional>
0
Returns:
Type: 
number

mutateColor(color, amountopt, alphaAmountopt) → {Color}

Returns a new color that has each component randomly adjusted

Parameters:
NameTypeAttributesDefaultDescription
colorColor
amountnumber<optional>
0.05
alphaAmountnumber<optional>
0
Returns:
Type: 
Color

randColor(colorAopt, colorBopt, linearopt) → {Color}

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

Parameters:
NameTypeAttributesDefaultDescription
colorAColor<optional>
WHITE
colorBColor<optional>
BLACK
linearboolean<optional>
false
Returns:
Type: 
Color

sign() → {number}

Randomly returns either -1 or 1 deterministically

Returns:
Type: 
number

vec2(valueAopt, valueBopt) → {Vector2}

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
Parameters:
NameTypeAttributesDefaultDescription
valueAnumber<optional>
1
valueBnumber<optional>
0
Returns:
Type: 
Vector2