samedi 16 mars 2019

Why is Random.nextInt() and Math.random going out of the range I set?

So I needed to get random numbers between 25 and 75 and I tried two things:

System.out.println(random.nextInt(75)+25);
System.out.println((int)(Math.random()*75) + 25);

From what I understood, the "75" I wrote in both lines should be the greatest number I get and the "25" should be the smallest number. However, I got numbers like:

84,94,82,79,98 // these are only the numbers that went out of range

I guess I misunderstood the rules of nextInt and Math.random. How can I get the numbers I want(between 25 and 75)?




Aucun commentaire:

Enregistrer un commentaire