I am trying to create a large random number without the need for cryptographic security. Therefore, I have opted not to use crypto.getRandomValues
. Currently, my method of generating the random number is 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, I feel like this approach may be inefficient because JavaScript numbers are inherently 64 bits long:
I believe it should be possible to extract all 52 bits of the mantissa at least. How many bits of entropy can we derive from numbers generated by Math.random
in JavaScript, and what would be the best method to do so?