javascript中的Math.random是如何实现随机性的? [英] How is randomness achieved with Math.random in javascript?

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

问题描述

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

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.

推荐答案

来自规范:

随机():

返回一个带有正数的数值符号,大于或等于 0 但小于 1,随机或伪选择随机且近似均匀在该范围内分布,使用依赖于实现的算法或战略.这个功能不需要参数.

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.

所以答案是这取决于您使用的 JavaScript 引擎.

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天全站免登陆