mercredi 8 mars 2017

Random numbers between a range, why do you add the minimum bound at the end?

So I know how to generate random numbers, but I'm really unclear about why you need to add the minimum bound onto the end... in some limited testing, it doesn't seem to make any difference.

Sorry if this is unclear, here is what I'm talking about:

return((b-a)*rand.nextDouble()+a);

Why are we adding the minimum, a, back on at the end? I ran 10 tests, using -5 as the minimum and 5 as the maximum, and the numbers seem generally to be randomly distributed in the same way no matter what the +a value is changed to... here for example,

return((b-a)*rand.nextDouble()-43534545);

And the tests return

1.2919329716135852
-0.590819166589295
3.583711305684826
-0.1820454980207078
2.090228188929454
-2.5879511577336833
-1.913608477301012
1.1121105506885156
4.310706334105493
-3.7728897127138694

So what exactly is the purpose?

Aucun commentaire:

Enregistrer un commentaire