Issue
The code below is supposed to generate random integers from 97 till 122:
for(int x = 0; x < 10; x++) {
int a = (int)(Math.random()*(26 + 97));
System.out.println(a);
}
The outputs I am getting are all over the place. They go below 97. Here are the outputs for one of the runs:
33
113
87
73
22
25
118
29
16
21
Solution
It's a paren problem. Try this instead.
(int)(Math.random()*26)
gives a number between 0 and 25 inclusive.
Add 97 to that range and you get between 97 and 122 inclusive.
int a = (int)(Math.random()*26) + 97;
Answered By - WJS
Answer Checked By - Robin (JavaFixing Admin)