mardi 27 octobre 2020

What is wrong with this code for generating random integers in Java??? (Unexpected Outputs)

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



Aucun commentaire:

Enregistrer un commentaire