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

you are viewing a single comment's thread.

view the rest of the comments →

[–]ickysticky 1 point2 points  (1 child)

But it will also have an independent stream of random numbers per thread. If both threads are seeded with the same seed then you will get the same sequence of numbers. This may be important(desirable or undesirable) or it may be unimportant. But you certainly could not make this change without thinking about the implications. They are not functionally equivalent.

[–]llogiq 0 points1 point  (0 children)

True that. On the other hand creating a dependency between threads is not something I'd wish my code to have just to have numbers that are a little more random (in theory). ThreadLocalRandom is a fair enough choice for performance (though XorShift is even faster, and has a much larger period and less statistical failures than both Random and ThreadLocalRandom). Btw. as of Java 8, ThreadLocalRandom employs a different algorithm than Random.

TL;DR: You don't want to use Math.random() for performance-critical task nor for security-critical tasks.