3 ms·
I apply the above theory for generating random numbers in JavaScript, based on given inputs (server seed, client seed, and nonce). JavaScript uses double precis
by EthicalSimilar 3y ago
I apply the above theory for generating random numbers in JavaScript, based on given inputs (server seed, client seed, and nonce). JavaScript uses double precision floating point numbers, meaning 52 bits for the mantissa.
const hash = crypto
.createHmac('SHA256', serverSeed)
.update(`${ clientSeed }:${ nonce }`)
.digest('hex');
const significant = hash.substring(0, 52 / 4);
const integer = parseInt(significant, 16);
const float = integer / (2 * 52);
return { float, hash, };