LittleJS 1.30.0

Random

Random global functions

Functions

Functions

rand(valueA, valueB) → numberengineMath.js:493

Returns a random value between the two values passed in

valueA number optional, default 1
valueB number optional, default 0
randBool(chance) → booleanengineMath.js:507

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

chance number optional, default 0.5
randColor(colorA, colorB, linear) → ColorengineMath.js:587

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
randInCircle(radius, minRadius) → Vector2engineMath.js:525

Returns a random Vector2 within a circular shape

radius number optional, default 1
minRadius number optional, default 0
randInt(valueA, valueB) → numberengineMath.js:501

Returns a floored random value between the two values passed in The upper bound is exclusive. (If 2 is passed in, result will be 0 or 1)

valueA number
valueB number optional, default 0
randPick(items) → T|undefinedengineMath.js:541

Returns a random element of an array or a Set, each as likely, undefined when it is empty

items Array<T>|Set<T>
const enemy = randPick(['slime', 'bat', 'ghost']);
randSign() → numberengineMath.js:512

Randomly returns either -1 or 1

randVec2(length) → Vector2engineMath.js:518

Returns a random Vector2 with the passed in length

length number optional, default 1
randWeighted(items, weights) → T|undefinedengineMath.js:552

Returns a random element of an array or a Set, each picked in proportion to its weight, undefined when none has any weight

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

One for each item in order, 0 or more, 0 never picked

const drop = randWeighted(['coin', 'heart', 'gem'], [10, 3, 1]); // a gem 1 time in 14