Our os::random() implementation only returns 31 bits, the 32nd bit is always set to 0 because of our choice of implementation:
* next_rand = (16807*seed) mod (2**31-1)
If one wants to use it to choose between true/false, one might be tempted to use:
if (os::random() > 0) {
} else {
}
but that will not work, since the returned value will always be positive.
We should rename os::random() to os::random31bits(), and/or provide os::random_interval(int range)
Adding a comment would also be useful.
Hopefully, this would help to use this API correctly.
* next_rand = (16807*seed) mod (2**31-1)
If one wants to use it to choose between true/false, one might be tempted to use:
if (os::random() > 0) {
} else {
}
but that will not work, since the returned value will always be positive.
We should rename os::random() to os::random31bits(), and/or provide os::random_interval(int range)
Adding a comment would also be useful.
Hopefully, this would help to use this API correctly.
- is blocked by
-
JDK-8329968 os::random should be random
- Closed