使用Javascript的真或更好的随机数

rlb*_*usa 19 javascript random

我有各种依赖于javascript随机数的资源.但是,由于我生成随机数的方式,我已经看到很多问题,随机不是那么随机.

是否有任何javascript资源可以生成真实的,或者更好的随机数?

我知道我可以与Random.org交互,但我还有其他选择吗?

我正在使用:

function rand( lowest, highest){
    var adjustedHigh = (highest - lowest) + 1;       
    return Math.floor(Math.random()*adjustedHigh) + parseFloat(lowest);
}
Run Code Online (Sandbox Code Playgroud)

Phi*_*l H 18

假设您不仅仅看到没有任何模式,请尝试使用Mersenee Twister(维基百科文章).在github上有各种类似的实现.

类似的问题:

可种子JavaScript随机数生成器

如果你想要更接近真正随机的东西,那么考虑使用random.org API来获得真正的随机数,尽管我建议只使用它来播种,而不是每个数字,因为你需要遵守它们的使用限制.


Dav*_*ary 13

调整数字,使他们"看起来随机"

我同意Phil H的看法,人类非常善于发现他们常常认为他们看到模式的模式,即使是在"完全随机"的数字序列中(聚类幻觉,嗜睡,赌徒的谬误等).

真正随机位置的图通常有很多块和点"巧合地"非常接近,这看起来很可疑.

艺术家经常采用完全随机生成的模式并"轻推"它们以使它们看起来 "更随机",即使仔细轻推实际上使得模式不那么随机(a),(b),(c),(d)等.

或者,低差异序列有时"看起来比真正的随机序列更好",并且生成得快得多.

快速随机数发生器

整个频谱中有许多"随机数发生器",从"极快"到"相对慢",从"易于人类看到模式"到"不太可能无人帮助的人看不到任何模式"到"加密安全"据我们所知,在播种了足够数量的熵之后,与任何使用人类产生的能量少于一个月的攻击者无法区分."

非加密强度的随机数发生器仍能提供出色的输出(无人帮助的人不可能看到任何模式)包括Mersenne捻线机,乘法携带,滞后Fibonacci发生器,等分布的长周期线性,Xorshift等.

与某些浏览器兼容的加密随机数技术

我听说Cryptocat和其他JavaScript应用程序使用的方便window.crypto.getRandomValues()window.msCrypto.getRandomValues()或者SubtleCrypto.generateKey()被设计来生成加密随机数的功能.不幸的是,该功能在IE 11及以下版本中不可用.

由于Web浏览器一直使用随机数(对于他们获取的每个"https://"页面),这些函数(如果可用)很可能比用JavaScript编写的大多数随机数生成器运行得更快 - 甚至非加密算法.

密码随机数技术与古代和现代浏览器兼容

在JavaScript中生成真随机数的一种方法是捕获鼠标事件并将它们添加到熵池中,跟踪添加的熵的一些(希望是保守的)估计.一旦池"满"(估计表明已添加至少128位熵),使用一些加密安全随机数生成器从池中生成随机数 - 通常通过使用单向散列使得序列几千个输出数字不足以推断出熵池的状态,从而预测下一个输出数量.

一个实现:http://lightsecond.com/passphrase.html

进一步阅读


Luk*_*rms 7

在寻找 Math.random 的替代方案时,我偶然发现了这个问题。

虽然这些都是有效的答案,但对我有用的解决方案只是使用 Math.random 两次。
并对浮点数的小数位使用模数。
主要是为了增加随机性。

也许对于一些通过谷歌引导解决这个问题的人来说可能有用。

这是包含该函数的代码片段,并且运行了该函数一百万次。

function rand(min, max){
    return (Math.floor(Math.pow(10,14)*Math.random()*Math.random())%(max-min+1))+min;
}

// rolling the rand
function rollRands(min, max, rolls) {
    let counts = {};
    for(let i = min; i <= max; i++) {
        counts[i] = 0
    }
    let roll = 0;
    while (roll < rolls) {
        counts[rand(min,max)]++;
        roll++;
    }
    return counts;
}

// testing a million rands
console.log(rollRands(36, 42, 1000000));
Run Code Online (Sandbox Code Playgroud)


Aar*_*zyk 5

Rando.js具有加密安全性。它基本上window.crypto.getRandomValues()用作window.msCrypto.getRandomValues()故障保护和Math.random()最后手段的故障保护,但它更容易实现和使用。这是一个基本的加密安全随机 [0, 1) 数:

console.log(rando());
Run Code Online (Sandbox Code Playgroud)
<script src="https://randojs.com/2.0.0.js"></script>
Run Code Online (Sandbox Code Playgroud)

好,易于。如果这就是您想要的,那么您就可以开始了。如果您希望它为您做更多事情,它也可以实现以下所有功能:

console.log(rando(5));                      //an integer between 0 and 5 (could be 0 or 5));  
console.log(rando(5, 10));                  //a random integer between 5 and 10 (could be 5 or 10));  
console.log(rando(5, "float"));             //a floating-point number between 0 and 5 (could be exactly 0, but never exactly 5));  
console.log(rando(5, 10, "float"));         //a floating-point number between 5 and 10 (could be exactly 5, but never exactly 10));  
console.log(rando(true, false));            //either true or false  
console.log(rando(["a", "b"]));             //{index:..., value:...} object representing a value of the provided array OR false if array is empty  
console.log(rando({a: 1, b: 2}));           //{key:..., value:...} object representing a property of the provided object OR false if object has no properties  
console.log(rando("Gee willikers!"));       //a character from the provided string OR false if the string is empty. Reoccurring characters will naturally form a more likely return value  
console.log(rando(null));                   //ANY invalid arguments return false  

//Prevent repetitions by grabbing a sequence and looping through it
console.log(randoSequence(5));              //an array of integers from 0 through 5 in random order  
console.log(randoSequence(5, 10));          //an array of integers from 5 through 10 in random order  
console.log(randoSequence(["a", "b"]));     //an array of {index:..., value:...} objects representing the values of the provided array in random order  
console.log(randoSequence({a: 1, b: 2}));   //an array of {key:..., value:...} objects representing the properties of the provided object in random order  
console.log(randoSequence("Good gravy!"));  //an array of the characters of the provided string in random order  
console.log(randoSequence(null));           //ANY invalid arguments return false
Run Code Online (Sandbox Code Playgroud)
<script src="https://randojs.com/2.0.0.js"></script>
Run Code Online (Sandbox Code Playgroud)

它也支持使用 jQuery 元素,但我在这个演示中省略了它,这样我就不必在 jQuery 中获取源代码。如果您需要,只需在GitHub网站上查看即可。


归档时间:

查看次数:

25995 次

最近记录:

8 年 前