-3

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?

Mauro Midolo
  • 1,841
  • 3
  • 14
  • 34
  • Googling the question title gives a great answer in the very first link. This should get downvoted into oblivion for "Lack of research effort" – Ordous Sep 04 '14 at 11:56

2 Answers2

1

Since Math.random() returns a number between 0 and 1 you can use a type like:

double d = 65 + Math.random()*(122-65);
Eypros
  • 5,370
  • 6
  • 42
  • 75
1

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();
Chip
  • 382
  • 2
  • 9