【问题标题】:Random number generator to array, with no duplicates java [duplicate]随机数生成器到数组,没有重复java [重复]
【发布时间】:2016-02-27 21:01:53
【问题描述】:

所以我正在尝试制作一个程序,该程序生成一个包含 20 个随机数的数组,并且没有重复项(对于最终用户)。到目前为止,这是我的代码

import java.util.*;
public class randomprog
{
public static void main(String args[])     
{
    Random rand = new Random();
    int[] list = new int[20];
    boolean generating=true;
    int counting=0;
    while(generating)
    {
        int testNum= rand.nextInt(30)+1;
        if (Arrays.asList(list).contains(testNum))
        {}
        else
        {
            list[counting]=testNum;
            counting++;
            System.out.println(testNum);
        }
        if(counting>=20)
        {
            generating=false;
        }
    }
}}

如您所见,我已经尝试过使用 Arrays.asList(list).contains(mynumber) 但是我仍然在输出中收到重复的内容,例如 29 4 4 1 20 30 20 23 30 11 6 7 27 14 16 8 4 19 7 15

有什么建议吗?

【问题讨论】:

    标签: java arrays random


    【解决方案1】:

    使用 HashSet 来跟踪您使用过的数字。

    例如

    int[] result = new int[20];
    
    HashSet<Integer> used = new HashSet<Integer>();
    for (int i = 0; i < 20; i++) {
        int add = (int)(Math.random() * 30); //this is the int we are adding
        while (used.contains(add)) { //while we have already used the number
            add = (int) (Math.random() * 30); //generate a new one because it's already used
        }
        //by this time, add will be unique
        used.add(add);
        result[i] = add;
    }
    

    这可确保您不会有重复项,并且也比在 ArrayList 中搜索要快得多,ArrayList 每次搜索数字时都会执行与 ArrayList 大小相等的操作。 HashSet 在检查是否包含数字时只执行 1 次操作。

    【讨论】:

    • 很高兴知道这一点,但对于四核 cpu,我认为 20 个整数的数组列表也应该没问题
    【解决方案2】:

    您的代码不起作用的原因是 Arrays.asList(int[] list) 返回大小为 1 的 ArrayList&lt;int[]&gt;,而不是 ArrayList&lt;Integer&gt;。因此,当您调用 contains 时,它不会检查原始列表的整数元素,而是始终返回 false

    【讨论】:

      【解决方案3】:

      我建议使用 ArrayList 并且不要使用空的 if 块。 这应该可以。

         import java.util.ArrayList;
          import java.util.List;
          import java.util.Random;
      
          public class Main {
                  static List list = new ArrayList<>(20);
                  public static void main(String args[])
                  {
                      Random rand = new Random();
                      boolean generating=true;
                      int counting=0;
                      while(generating)
                      {
                          int testNum= rand.nextInt(30)+1;
                          if (!list.contains(testNum))
                          {
                              list.add(testNum);
                              counting++;
                              System.out.println(testNum);
                          }
                          if(counting>=20)
                          {
                              generating=false;
                          }
                      }
                  }
      }
      

      【讨论】:

        【解决方案4】:

        使用 Collections.Shuffle

        class Ideone{
            public static void main(String[] args) {
            Integer[] arr = new Integer[20];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = i;
            }
            Collections.shuffle(Arrays.asList(arr));
            System.out.println(Arrays.toString(arr));
        
        }}
        

        【讨论】:

        • 改组数组如何删除重复项?洗牌不只是随机化顺序吗?
        • 他问“所以我正在尝试制作一个程序,该程序可以生成一个由 20 个随机数组成的数组,并且没有重复(对最终用户)”。
        • 洗牌不会删除重复项。
        • 该数组将包含从 0 到 19 的所有整数,以随机顺序排列。所以是的,这很可能就是我们所要求的。
        • 我同意你的看法。但他要求他需要创建 20 个不重复的随机数数组。以上代码 sn-p 将完成工作。
        猜你喜欢
        • 2016-03-01
        • 1970-01-01
        • 2021-06-01
        • 1970-01-01
        • 2016-05-15
        • 2016-05-05
        相关资源
        最近更新 更多