如何在JavaScript中使用Math.random实现随机性? [英] How is randomness achieved with Math.random in javascript?

查看:120
本文介绍了如何在JavaScript中使用Math.random实现随机性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在javascript中使用Math.random实现随机性?我已经做了一些随机选择大约50种不同选项的东西。我想知道使用Math.random来获得随机性是多么的舒服。

解决方案

根据规格:


random():



返回带有正
符号的Number值,大于或等于0但
小于1,随机选择或伪
随机使用
依赖于实现的算法或
策略,在该范围内大致统一
分布。这个函数没有
参数。


所以答案是它依赖于你正在使用的JavaScript引擎。 / p>

我不确定所有浏览器是否使用相同的策略或不幸的策略



它应该是很适合你的目的。只有你做了大量的数字才会开始看到一个模式


How is randomness achieved with Math.random in javascript? I've made something that picks between around 50 different options randomly. I'm wondering how comfortable I should be with using Math.random to get my randomness.

解决方案

From the specifications:

random():

Returns a Number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an implementation-dependent algorithm or strategy. This function takes no arguments.

So the answer is that it depends on what JavaScript engine you're using.

I'm not sure if all browsers use the same strategy or what that strategy is unfortunately

It should be fine for your purposes. Only if you're doing a large amount of numbers would you begin to see a pattern

这篇关于如何在JavaScript中使用Math.random实现随机性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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