I would like to get a random value between 65 to 122.
How may I do that with the help of Math.random();
in Java?
Is there any way to bound the values that will be accepted?
I would like to get a random value between 65 to 122.
How may I do that with the help of Math.random();
in Java?
Is there any way to bound the values that will be accepted?
Since Math.random()
returns a number between 0 and 1 you can use a type like:
double d = 65 + Math.random()*(122-65);
The static method Math.random()
returns a number between 0 and 1 so you just have to multiply the result with the difference between you minimal and maximal value and add this to your minimal value.
int min = 65;
int max = 122;
int random = (int) min + (max - min) * Math.random();