jeudi 7 janvier 2016

Using (Math.random() * ((upperbound - lowerbound) + 1) + lowerbound) in JAVA generates a number out of the specified range [duplicate]

This question already has an answer here:

I know how this works in principle:

    (Math.random() * ((upperbound - lowerbound) + 1) + lowerbound);

But if I use this for negative numbers for example: I want to generate random numbers ranging from -30 to -35, I use the same formula:

    (Math.random()*(-30-(-35)+1)+(-35));

But the generated numbers contain -29 too. Is there a way to avoid this ?




Aucun commentaire:

Enregistrer un commentaire