mardi 27 septembre 2016

Java Random.nextDouble() probability

I'm trying to get random double numbers between 1.0 and 10.0 using the Random.nextDouble() as such:

double number = 1.0 + (10.0-1.0) * Random.nextDouble();

Simulating this 10,000 times I find out that the probability of 1.0 and 10.0 is lower than the rest. The only way to get these number would require Random.nextDouble() to be either 0.0 or 1.0. So essentially since Random.nextDouble() gives a random number inbetween 0.0 and 1.0 it's less likely to actually result in 0.0 or 1.0.

Is there a way to mend this I wonder? Or should I try a completely different way as a whole?




Aucun commentaire:

Enregistrer un commentaire