随机元素生成没有重复Java

时间:2016-02-05 15:09:25

标签: java arrays duplicates

我正在尝试让这些代码在没有重复的情况下运行但是没有成功研究这个领域。

它是我正在做的问题的开始,它将要求用户输入缺少的元素。但是,当我生成随机元素时,我得到重复

import java.util.Random;

public class QuestionOneA2 {

    public static void main(String[] args) {

        String[] fruit = {"orange", "apple", "pear", "bannana", "strawberry", "mango"};
        Random numberGenerator = new Random();

        for (int i = 0; i < 5; i++) {
            int nextRandom = numberGenerator.nextInt(6);
            System.out.println(fruit[nextRandom]);
        }


    }

}

9 个答案:

答案 0 :(得分:1)

当您生成随机数时,我建议将其添加到数组中。

然后,当您生成下一个号码时,请进行某种搜索(谷歌搜索有效的内容)以检查该号码是否已经在数组中,因此已经使用过。

如果是,则生成一个新的,如果不是,则使用它。

您可以通过将其嵌套在while循环中来完成此操作。

虽然从我的问题可以看出,你最好只使用ArrayList创建一个水果数组的副本,然后,当你生成一个随机数来选择一个水果时,只需从这个中删除这个水果新列表并减少您生成的随机数范围。

答案 1 :(得分:1)

您可以考虑许多不同的方法,具体取决于算法的灵活程度。

从6个列表中取5个随机元素,与6个列表中的选择1元素相同,您不选择。这是非常不灵活的,但很容易。

另一种方法可能是从列表中删除元素,并减少最大随机数。在这个原因中,我建议不要使用String[]

答案 2 :(得分:1)

您可以使用Set来验证随机生成的数字是否重复。您必须继续生成randomNumber,直到找到唯一的随机数,然后将其添加到Set以防止重复。

以下是快速代码段:

public static void main(String[] args) {
    String[] fruit = {"orange", "apple", "pear", "bannana", "strawberry", "mango"};
        Random numberGenerator = new Random();
        /* Generate A Random Number */
        int nextRandom = numberGenerator.nextInt(6);
        Set<Integer> validate = new HashSet<>();
        /* Add First Randomly Genrated Number To Set */
        validate.add(nextRandom);
        for (int i = 0; i < 5; i++) {
            /* Generate Randoms Till You Find A Unique Random Number */
            while(validate.contains(nextRandom)) {
                nextRandom = numberGenerator.nextInt(6);
            }
            /* Add Newly Found Random Number To Validate */
            validate.add(nextRandom);
            System.out.println(fruit[nextRandom]);
        }
}

输出:

mango
apple
strawberry
pear
orange

答案 3 :(得分:0)

将您的数组复制到List<String>,然后将其随机播放,然后逐个选择元素:

List<String> copy = new ArrayList<>(Arrays.asList(fruit));
Collections.shuffle(copy);
for (String f : copy)
    System.out.println(f);

答案 4 :(得分:0)

如果我理解正确,那么你想从n个元素的列表中随机选择n-1个元素。如果是,那么我建议随机选择一个,然后选择其他所有。

Arrays.shuffle(fruit);
int notThis = numberGenerator.nextInt(6);
for(int i = 0; i < fruit.length; i++)
    if(i!=notThis) System.out.println(fruit[i]);

答案 5 :(得分:0)

您可以将int包装到“Interger”中并将其添加到Set中。 Set不保留重复项,因此其中只有唯一值。那么只需检查Set是否已经使用Set.contains(Integer)给出了Integer。

答案 6 :(得分:0)

我的个人解决方案:

private static int[] randomIndexes(int len) {
    int[] indexes = new int[len];
    for (int i = 0; i < len; i++) {
        indexes[i] = i;
    }
    for (int i = len - 1, j, t; i > 0; i--) {
        j = RANDOM.nextInt(i);
        t = indexes[j];
        indexes[j] = indexes[i];
        indexes[i] = t;
    }
    return indexes;
}

查看实际操作:https://gist.github.com/GautierLevert/a6881cff798e5f53b3fb

答案 7 :(得分:0)

我认为使用ArrayList会更容易,并且还会控制随机数的生成,如下所示。

import java.util.Random;

public class QuestionOneA2 {

      public static void main(String[] args) {

        List<String> fruits = new ArrayList<>();

        fruits.add("orange");
        fruits.add("apple");
        fruits.add("pear");
        fruits.add("bannana");
        fruits.add("strawberry");
        fruits.add("mango");

        Random numberGenerator = new Random();
        int nextRandom;

       for (int i = 0; i < 6 ; i++) {
           nextRandom = numberGenerator.nextInt(6 - i);
           System.out.println(fruits.get(nextRandom));
           fruits.remove(nextRandom);
        }
      }

}

答案 8 :(得分:-1)

fruit.remove(fruit[nextRandom]);

也许,这是删除子方法吗?

相关问题