真或更好的 Javascript 随机数 [英] True or better Random numbers with Javascript

查看:13
本文介绍了真或更好的 Javascript 随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有各种依赖于 javascript 随机数的资源.但是,由于我生成随机数的方式,我发现了很多问题,其中随机不是那么随机.

I have all kinds of resources that rely on javascript random numbers. However, I've been seeing a lot of problems where random isn't so random because of the way I'm generating random numbers.

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

Is there any javascript resource for me to generate true, or just better random numbers?

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

I know that I can interface with Random.org, but what other options do I have?

我正在使用:

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

推荐答案

假设您不只是看到没有任何模式的模式,请尝试使用 Mersenee Twister (维基百科文章在这里).有各种实现,例如 github 上的这个.

Assuming you're not just seeing patterns where there aren't any, try a Mersenee Twister (Wikipedia article here). There are various implementations like this one on github.

类似的问题:

可播种的 JavaScript 随机数生成器

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

If you want something closer to truly random, then consider using the random.org API to get truly random numbers, although I would suggest only using that to seed, not for every number, as you need to abide by their usage limits.

这篇关于真或更好的 Javascript 随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆