Random

A pseudo-random number generator (PRNG).

Table of Contents

getSeed(​)

Get the current seed value of the random number generator.

getSeed Return value

Number. The seed value.

rand(​)

Return a random number between 0 (inclusive) and 1 (exclusive).

rand Return value

Number. Random number between 0 and <1.

randInt(​min, max[, randFn])

Return a random integer between a minimum (inclusive) and maximum (inclusive) integer.

let { randInt, rand } = kontra;

// random number between 10 and 20
console.log( randInt(10, 20) );

// bias the result of the random integer to be closer
// to the max
console.log( randInt(10, 20, () => rand() ** 2) );
import { randInt, rand } from 'path/to/kontra.mjs';

// random number between 10 and 20
console.log( randInt(10, 20) );

// bias the result of the random integer to be closer
// to the max
console.log( randInt(10, 20, () => rand() ** 2) );
import { randInt, rand } from 'kontra';

// random number between 10 and 20
console.log( randInt(10, 20) );

// bias the result of the random integer to be closer
// to the max
console.log( randInt(10, 20, () => rand() ** 2) );

randInt Parameters

min

Number. Min integer.

max

Number. Max integer.

randFn Optional

Function. Function that generates a random number. Useful for biasing the random number.

randInt Return value

Number. Random integer between min and max values.

seedRand(​[value])

Initialize the random number generator with a given seed.

let { seedRand, rand } = kontra;

seedRand('kontra');
console.log(rand()); // => always 0.26133555523119867
import { seedRand, rand } from 'path/to/kontra.mjs';

seedRand('kontra');
console.log(rand()); // => always 0.26133555523119867
import { seedRand, rand } from 'kontra';

seedRand('kontra');
console.log(rand()); // => always 0.26133555523119867

seedRand Parameters

value Optional

Number or String. Number or string to seed the random number generator. Defaults to Date.now().