Unknown user Unknown user - 1 month ago 9
Java Question

Getting random numbers in Java


Possible Duplicate:

Java: generating random number in a range




I would like to get a random value between 1 to 50 in Java.

How may I do that with the help of
Math.random();
?

How do I bound the values that Math.random() returns?

Answer
Random rand = new Random();

int  n = rand.nextInt(50) + 1;
//50 is the maximum and the 1 is our minimum