生成随机数数组并替换重复项

Generate Random Number Array and Replace Duplicates

我正在生成 1-54 范围内的 6 个整数的数组(类似于乐透号码)。但是,当尝试检测重复项时,我的代码没有执行。如果我的条件逻辑会捕获重复项,我尝试通过简单地打印 "WE HAVE FOUND A DUPLICATE" 来进行调试。当然,它不会打印所需的输出:

18-33-39-41-41-45

我想检测重复项,以便生成另一个不等于发现的重复项的随机数。我不想只是 increment/decrement 重复值只是为了得到另一个数字,我想实际生成另一个 1 和 54 之间不等于重复值的值。

这是我的代码:

public class Main {

    public static void main(String[] args) {

        Random rand = new Random();
        int[] luckyArray = new int[6];

        //build array
        for (int i = 0; i < luckyArray.length; i++) {

            int luckyNumber = rand.nextInt(54) + 1;
            luckyArray[i] = luckyNumber;
        }
        //detect duplicates in the array
        for (int i = 0; i < luckyArray.length; i++) {
            if (i > 0 && luckyArray[i] == luckyArray[i - 1]) {
                System.out.print("WE HAVE FOUND A DUPLICATE!");
                /* while (luckyArray[i - 1] == luckyArray[i]) {
                    luckyArray[i] = rand.nextInt(54) + 1;
                }*/
            }
        }
        //sort the array before printing
        Arrays.sort(luckyArray);
        for (int t = 0; t < luckyArray.length; t++) {

            if (t != 5) {
                System.out.print(luckyArray[t] + "-");
            } else System.out.print(luckyArray[t]);
        }
    }
}

我希望将当前索引 [i] 与其之前的索引 [i-1] 和 if/when 进行比较,它们相等,为当前索引生成一个新值。

提前致谢!

我(个人)会在这里依赖 Set 的属性来确保您根本不会保留重复项 - 您甚至不必检查是否有重复项:

    Random rand = new Random();
    int numNumsToGenerate = 6;
    Set<Integer> luckySet = new HashSet<Integer>();

    //build set
    while (luckySet.size() < numNumsToGenerate) {
        luckySet.add(rand.nextInt(54) + 1);
    }

建立集合后,您可以通过使用 Set:

的内容创建一个新的 List 来对它们进行排序
//sort the array before printing
List<Integer> luckyList = new ArrayList<Integer>(luckySet);
Collections.sort(luckyList);

与Java 8:

List<Integer> luckys = 
    IntStream.rangeClosed(1, 54)
             .boxed()
             .collect(collectingAndThen(toList(), l -> {
                 Collections.shuffle(l);
                 return l;
             }))
             .stream()
             .limit(6)
             .collect(toList())

在检查重复项之前,将 'sort' 上移。这样你就可以确保重复项总是彼此相邻,你只需要检查邻居。

这里是检测重复的代码:

package luckynumber;

import java.util.Arrays;
import java.util.Random;

public class Main {

    public static void main(String[] args) {

        Random rand = new Random();
        int[] luckyArray = new int[6];

        // build array
        for (int i = 0; i < luckyArray.length; i++) {

            int luckyNumber = rand.nextInt(54) + 1;
            luckyArray[i] = luckyNumber;
        }
        // sort the array before detecting duplicates
        Arrays.sort(luckyArray);
        // detect duplicates in the array
        for (int i = 0; i < luckyArray.length; i++) {
            if (i > 0 && luckyArray[i] == luckyArray[i - 1]) {
                System.out.print("WE HAVE FOUND A DUPLICATE!");
                /* while (luckyArray[i - 1] == luckyArray[i]) { luckyArray[i] =
                 * rand.nextInt(54) + 1; } */
            }
        }
        for (int t = 0; t < luckyArray.length; t++) {

            if (t != 5) {
                System.out.print(luckyArray[t] + "-");
            } else
                System.out.print(luckyArray[t]);
        }
    }
}

当您现在找到重复项并更改数组项时,请务必重新排序并重新检查整个数组。 剩下的这一步留作 reader 的练习。 ;-)

顺便说一句:使用 Set 会使代码更简单。

如果集合不是一个选项,您将需要进行检查以每次遍历您的数组。您当前的检查仅比较序列号 1&2、2&3 等

或者将要检查的幸运数字分配给一个 int,以便在插入之前对照数组进行检查。

这是一个使用 Java 8 个流的非常简单的解决方案:

int[] luckyArray = new Random().ints(1, 55).distinct().limit(6).sorted().toArray();

这不会手动检测重复项 - 而是使用 distinct 在选择前 6 个然后对它们进行排序之前删除它们。