This question already has an answer here:
While reading the documentation of nextInt(int)
method in Random
class, i didn't understand what is the meaning of the following paragraph :
The algorithm is slightly tricky. It rejects values that would result in an uneven distribution (due to the fact that 2^31 is not divisible by n). The probability of a value being rejected depends on n. The worst case is n=2^30+1, for which the probability of a reject is 1/2, and the expected number of iterations before the loop terminates is 2.
can anyone give me an example of a number that will be rejected by nextInt(int)
method ?
Source Random class documentation.
Aucun commentaire:
Enregistrer un commentaire