vendredi 17 mars 2017

Generating random numbers, doesn't return some numbers

Just trying to generate 8 random numbers, but I'm only getting 3 numbers returned.

Code:

import java.util.Random;

public class One{
  public static void main(String[] args){

      Random rand = new Random();
      int[] deciJunc = new  int[8];

      for(int i=0; i<8; i++){
          deciJunc[i] = 1+rand.nextInt(8);
          System.out.println(deciJunc[i]);
      }

  }
}

Output:

3
5
7
3
3
5
7
3

Output:

3
3
3
5
7
7
7
3

Now, I've ran this program like 10 times and it's only giving me these 3 numbers (3,5,7) and I don't understand what's wrong. If I change:

deciJunc[i] = 1+rand.nextInt(8);

to

deciJunc[i] = 4+rand.nextInt(8);

It works fine, I get an output of a good mix like:

8
9
10
4
4
9
9
6

What's wrong with the first one where it only returns the 3 different numbers?




Aucun commentaire:

Enregistrer un commentaire