We had been using Math.random to get random numbers between 4000-64000.:
Math.floor(Math.random() * 60000 + 4000);
We have to now replace this
A simple replacement for Math.random
might look like this:
/**
* Return values in the range of [0, 1)
*/
const randomFloat = function () {
const int = window.crypto.getRandomValues(new Uint32Array(1))[0]
return int / 2**32
}
To extend this to integers:
/**
* Return integers in the range of [min, max)
*
* @todo check that min is <= max.
*/
const randomInt = function (min, max) {
const range = max - min
return Math.floor(randomFloat() * range + min)
}
To extend this to arrays of integers:
/**
* Generate an array of integers in the range of [min, max).
*/
const randomIntArray = function (length, min, max) {
return new Array(length).fill(0).map(() => randomInt(min, max))
}
Generate an array of ten integers from 0 to 2 inclusive:
randomIntArray(10, 0, 3)
[0, 2, 1, 2, 0, 0, 1, 0, 1, 0]