What is the difference between Math.random() * n
and Random.nextInt(n)
where n
is an integer?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
According to https://forums.oracle.com/forums/thread.jspa?messageID=6594485�
Random.nextInt(n)
is both more efficient and less biased thanMath.random() * n
another important point is that Random.nextInt(n) is repeatable since you can create two Random object with the same seed. This is not possible with Math.random().
According to this example
Random.nextInt(n)
has less predictable output then Math.random() * n. According to [sorted array faster than an unsorted array][1] I think we can say Random.nextInt(n) is hard to predict.usingRandomClass : time:328 milesecond.
usingMathsRandom : time:187 milesecond.
Here is the detailed explanation of why "
Random.nextInt(n)
is both more efficient and less biased thanMath.random() * n
" from the Sun forums post that Gili linked to: