Math.random()是否均匀生成伪随机数? [英] Does Math.random() generate pseudorandom numbers uniformly?

查看:731
本文介绍了Math.random()是否均匀生成伪随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此评论中,问题,dimo414指出,OP代码的问题之一是,它将不均匀地生成随机数.所以我想知道为什么会这样吗?特定算法是固有的吗?还是关于Math.random()本身的东西?是因为间隔[0.0,1.0)中选择的数字的浮点表示吗?

In this comment in another question, dimo414 states that one of the problems with the OP's code is that it will generate random numbers non-uniformly. So I wonder why this is? Is it inherent the the particular algorithm? Or is it something about Math.random() itself? Is it because of the floating-point representation of the numbers chosen in the interval [0.0, 1.0)?

p.s.我了解使用Random.nextInt()的建议答案.我还想进一步了解使用Math.random()的缺点.

p.s. I understand the proposed answer to use Random.nextInt(). I also want to know more about the flaws of using Math.random().

推荐答案

Java的Math.random()java.util.Random实例,但是未记录如何使用该实例生成随机数.例如,没有记录该方法是否调用

Java's Math.random() is documented to return numbers that "are chosen pseudorandomly with (approximately) uniform distribution from" the interval [0, 1). However, although that documentation specifies that it uses its own java.util.Random instance, it's not documented how it uses that instance to generate random numbers. For example, it's not documented whether that method will call the nextDouble method, which is exactly specified for java.util.Random.

这篇关于Math.random()是否均匀生成伪随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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