This question has already been answered:

It is necessary to fill an array of 9 cells with numbers from 1 to 9 so that they do not repeat.

1 option.

int[]ar = new int[9]; ar[0] = (int)(Math.random() * 9 + 1); ar[1] = (int)(Math.random() * 9 + 1); if (ar[1] == ar[0]) { ar[1] = (int)(Math.random() * 9 + 1); } ar[2] = (int)(Math.random() * 9 + 1); if (ar[2] == ar[1] || ar[2] == ar[0]) { ar[2] = (int)(Math.random() * 9 + 1); } ar[3] = (int)(Math.random() * 9 + 1); if (ar[3] == ar[2] || ar[3] == ar[1] || ar[3] == ar[0]) { ar[3] = (int)(Math.random() * 9 + 1); } ar[4] = (int)(Math.random() * 9 + 1); if (ar[4] == ar[3] || ar[4] == ar[2] || ar[4] == ar[1] || ar[4] == ar[0]) { ar[4] = (int)(Math.random() * 9 + 1); } ar[5] = (int)(Math.random() * 9 + 1); if (ar[5] == ar[4] || ar[5] == ar[3] || ar[5] == ar[2] || ar[5] == ar[1] || ar[5] == ar[0]) { ar[5] = (int)(Math.random() * 9 + 1); } ar[6] = (int)(Math.random() * 9 + 1); if (ar[6] == ar[5] || ar[6] == ar[4] || ar[6] == ar[3] || ar[6] == ar[2] || ar[6] == ar[1] || ar[6] == ar[0]) { ar[6] = (int)(Math.random() * 9 + 1); } ar[7] = (int)(Math.random() * 9 + 1); if (ar[7] == ar[6] || ar[7] == ar[5] || ar[7] == ar[4] || ar[7] == ar[3] || ar[7] == ar[2] || ar[7] == ar[1] || ar[7] == ar[0]) { ar[7] = (int)(Math.random() * 9 + 1); } ar[8] = (int)(Math.random() * 9 + 1); if (ar[8] == ar[7] || ar[8] == ar[6] || ar[8] == ar[5] || ar[8] == ar[4] || ar[8] == ar[3] || ar[8] == ar[2] || ar[8] == ar[1] || ar[8] == ar[0]) { ar[8] = (int)(Math.random() * 9 + 1); } 

it's good that there are only 9 of them.

Option 2.

 int[]ar = new int[8]; for (int i = 0; i < ar.length; i++) { i = (int)(Math.random() * 9 + 1); if (ar[i] = ar[i]) { ??? } } 

In the first version there is a problem: if 2 numbers match, then it changes the second, but if it matches the second time, it does not change. For example, 4-6-5-3 fell 4-6-5-3 and 6 came, he would change it, for example, by 4 , and 4 no longer change. How to do?

I don’t have to decide for me, please give me a thought.

Reported as a duplicate by Regent , user194374, Grundy , AK , pavel participants on Jan 7 '17 at 13:09 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • The inside body of the for loop is deleted. Declare a boolean variable b. Next is a while loop with the condition tied to b, in the body of which you set b to 0 (let's say) and generate a random number. Also in while doing another for loop, which runs through all the numbers in the array and compares with the generated number. If there is at least one match, the value of b should change to 1. Ie, until you generate a number unlike any other, you should not leave the while loop. - Sanek Zhitnik
  • store the generated values ​​in a separate container and compare - Sublihim
  • @Sublihim Why duplicate stored values? - AK
  • @AK, and who is talking about duplication? - Sublihim

2 answers 2

To use the HashSet collection, the elements of the collection contain only unique values ​​and repeats are excluded. New numbers will be generated until all the cells are filled:

 Set<Integer> generated = new HashSet<Integer>(); Random r = new Random(); while (generated.size() < 9) { generated.add(r.nextInt(9) + 1); } 

based on that answer

or shuffle() (mixing elements of the collection) arranged in order of numbers:

 List<Integer> fill = new ArrayList<>(); for ( int i = 0; i < 10; i++ ) { fill.add(i + 1); } Collections.shuffle(fill); 

based on that answer

For 9 elements, this will not overload the system.

If it is necessary in the array int[] , then the conversion from the collection is not complicated (for the second example, it is similar)

Using Apache Commons:

 int[] arr = ArrayUtils.toPrimitive(generated.toArray(new Integer[0])); 

or in Java 8:

  int[] arr = generated.stream().mapToInt(i -> i).toArray(); 

Other conversion options

    The general idea is quite simple - we generate the first number, add it to the array, generate the second number, check its presence in the array (using a loop, for example), if the generated number is already contained in the array, generate the following, if not contained, add it there.

    I will offer this option:

     final int N = 9; ArrayList<Integer> arrayList = new ArrayList<>(N); Random random = new Random(); while (arrayList.size() < N) { int i = random.nextInt(N) + 1; if (!arrayList.contains(i)) { arrayList.add(i); } } int[] randomArray = arrayList.stream().mapToInt(i -> i).toArray(); 

    This option is the most trivial: create an ArrayList<Integer> and fill it with 9 pseudo-random numbers. If the next generated number is already in the list, skip it and generate the following.

    In this variant, on average , the list is filled in 25 iterations of the loop.

    As an alternative - you can use any collection that implements the Set interface - they store only non-repeating elements.

    Fundamentally another option is to fill the list with elements in order, and then mix them:

     final int N = 9; ArrayList<Integer> arrayList = new ArrayList<>(N); for (int i=1; i<=N; i++) { arrayList.add(i); } Random random = new Random(); for (int i=0; i<N; i++) { int j = random.nextInt(N); if (j != i) { int tmp = arrayList.get(i); arrayList.set(i, arrayList.get(j)); arrayList.set(j, tmp); } } int[] randomArray = arrayList.stream().mapToInt(i -> i).toArray(); 

    As I understand it, the task is educational, so the simplest code for mixing the elements was cited above. But in practice it is better to use something ready, for example, Collections.shuffle(...) .

    • Regarding the second option: 1. Why do you use ArrayList instead of int[] ? 2. Why is K not dependent on N ? 3. Why in both random.nextInt "magic constant" 9 instead of N ? 4. From the point of view of probability theory, your mixing does not work correctly, because not all options for placing numbers in an array are equally probable. - Regent
    • @Regent, 1. Habit. And yes, here the array is enough. 2. I agree. The K == N option is sufficient. 3. Because at first N was not at all, corrected. - post_zeew
    • The first three points are questions about the quality of the code, so they are not critical. But the fourth point, which says about the incorrectness of the presented algorithm, in my opinion, is really important. Do you have plans to fix it? - Regent
    • @Regent, I changed the code. True, I don’t know exactly if this change affects your comment, but there will be no further code changes. - post_zeew
    • In terms of probability, it only got worse. You could just take the shuffle code from the answers to the question, a duplicate of which is this question, and not suffer. In my opinion, there must be a correct algorithm in the answer and received answer. - Regent