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?
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?
The first solution is Using
java.util.Random
class:Another solution is using
Math.random()
or
1. Using Math.random()
This will give you value from 1 to 50 in case of int or 1.0 (inclusive) to 50.0 (exclusive) in case of double
2. Using Random class in Java.
This will give value from 0 to 49.
For 1 to 50:
rand.nextInt((max - min) + 1) + min;
Source of some Java Random awesomeness.