mardi 9 mars 2021

Math.random() and Random::nextInt always give the same exact number inside a for loop

So I am trying to make an Array with random numbers, but whenever I try Math.random or create a new Random object and use it, I always get the same number multiple times. My code is this one:

int[] Array = new int[size];
for (int Y : Array) {
    Array[Y] = (int) (Math.random() * 10) + 3;
}

or this one:

int[] Array = new int[size];
for (int Y: Array) {
    Array[Y] = rand.nextInt(30); 
}

The output im getting is: [0][3][3][3][3][3][3][3][3][3][3][3][3][3][3][3][3][3][3][3][3][3][3][3][3][3][3][3][3][3]

I haven't set a seed and I tried it outside the loop and inside but still only get the same number.




Aucun commentaire:

Enregistrer un commentaire