I want to generate a really large random number. I don't need this number to be cryptographically secure. Hence, I'm not using crypto.getRandomValues. Currently, I'm generating the random number as follows:
const random = length =>
Math.floor(length * Math.random());
const padding = (length, character, string) =>
(new Array(length + 1).join(character) + string).slice(string.length);
const randomBits = bits =>
padding(bits, '0', random(Math.pow(2, bits)).toString(2));
const getRandom = bits =>
bits <= 32 ? randomBits(bits) : randomBits(32) + getRandom(bits - 32);
console.log(' 1 2 3 4 5 6');
console.log(getRandom(64));
However, this seems a bit wasteful because numbers in JavaScript are 64 bits long:
![]()
It seems to me that we should be able to at least recover all the 52 bits of the mantissa. How many bits of entropy can we extract from numbers generated by Math.random in JavaScript, and how?
A deterministic algorithm (including a pseudorandom number generator) can't generate entropy itself; it has to come from outside, such as the seed that the algorithm receives.
Note, however, that the ECMAScript specification for Math.random() allows the implementation to use any "implementation-dependent algorithm or strategy", not necessarily a deterministic algorithm, as long as the number is "chosen randomly or pseudo randomly with approximately uniform distribution over" the interval [0, 1). Thus, whether Math.random() actually uses entropy is likewise implementation-dependent — no particular strategy for gathering entropy to seed the PRNG (if the implementation uses one) is mandated either.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With