samedi 12 novembre 2016

How much entropy does Math.random generate?

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:

IEEE 754 double-precision binary floating-point format: binary64

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?




Aucun commentaire:

Enregistrer un commentaire