如何在Javascript中生成密码安全浮点数?
这应该是Math.random的插件,范围为(0,1),但在密码上是安全的.示例用法
cryptoFloat.random();
0.8083966837153522确保javascript中的随机数字安全?展示了如何创建加密安全的Uint32Array。也许这个可以被转换成浮子?
Float32Array.from(someUintBuf);总是给出一个完整的数字。发布于 2016-01-03 14:58:45
由于下面的代码非常简单,并且是函数等价于除法,所以这里是改变位元的交替方法。(这段代码是从@T.J.Crowder的非常有用的答案中复制和修改的)。
// A buffer with just the right size to convert to Float64
let buffer = new ArrayBuffer(8);
// View it as an Int8Array and fill it with 8 random ints
let ints = new Int8Array(buffer);
window.crypto.getRandomValues(ints);
// Set the sign (ints[7][7]) to 0 and the
// exponent (ints[7][6]-[6][5]) to just the right size
// (all ones except for the highest bit)
ints[7] = 63;
ints[6] |= 0xf0;
// Now view it as a Float64Array, and read the one float from it
let float = new DataView(buffer).getFloat64(0, true) - 1;
document.body.innerHTML = "The number is " + float;
解释:
IEEE754双级的格式为1符号位(ints[7][7]),11指数位(ints[7][6]到ints[6][5]),其余为尾数(包含值)。要计算的公式是

若要将因子设置为1,指数需要为1023。它有11位,因此最高阶位给出2048位.这需要设置为0,其他位设置为1。
https://stackoverflow.com/questions/34575635
复制相似问题