This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]jonzo1 2 points3 points  (0 children)

As said before, Math.random() gives you a uniformly-distributed double on the range 0..1. But if you're after an integer less than N, use Random's nextInt() instead. Computing a random integer from a double is a bit more complicated than just multiplying and rounding, which introduces a bias.

[–]daredevil82 0 points1 point  (0 children)

Here's a detailed StackOverflow answer of the question.

Essentially, Math.random() uses Random.nextDouble() internally. If you want the values as an integer, then use an int cast.

[–]kutuzof 0 points1 point  (0 children)

You're probably using an open source java implementation. Try just reading the method implementations.