【问题标题】:How to generate a list of random numbers between a certain range, then get numbers not included in that list如何生成一定范围内的随机数列表,然后获取未包含在该列表中的数字
【发布时间】:2019-11-27 14:24:42
【问题描述】:

我生成了一个包含 20 个介于 1 到 300 之间的唯一随机数的列表,并将其用于我需要的用途。但是,我还需要将未进入列表的数字添加到另一个列表中,以便在另一个函数中使用。

这是我用来生成 20 个数字的随机列表的代码:

JToggleButton[][] p = new JToggleButton[5][4];
ArrayList<Integer> list = new ArrayList<Integer>();

    Random rand = new Random();
    for (int i = 0; i < p.length; i++) {
        for (int j = 0; j < p[i].length; j++) {

            int randomNum = rand.nextInt((300 - 1) + 1) + 1;

            while (list.contains(randomNum)) {
                randomNum = rand.nextInt((300 - 1) + 1) + 1;
            }

            list.add(randomNum);

// rest of code that I need the random number list for

我需要将这 20 个项目列表中未包含的其他 280 个数字放入另一个列表中,但我不确定如何实际获取这些“未使用”的数字

【问题讨论】:

  • rand.nextInt((300 - 1) + 1) + 1 有点奇怪
  • @user8426627 你能告诉我为什么吗?
  • @user8426627 它实际上是指rand.nextInt((upperLimit - lowerLimit) + 1) + lowerLimit,这是有道理的,只是看起来很奇怪,因为所有东西都是1 并且没有变量。
  • IntStream.rangeClosed(1, 300).filter(item -&gt; !list.contains(item)).boxed().collect(Collectors.toList());?

标签: java random int


【解决方案1】:

您可以先创建一个包含所有 300 个数字的 List,然后当您将随机数添加到另一个列表时,只需从包含所有值的 List 中删除相同的数字:

创建列表:

    ArrayList<Integer> excludedNumbers = new ArrayList<>(300);
    for (int i = 1; i <= 300; i++){
        excludedNumbers.add(i);
    }

在当前代码中添加一行:

ArrayList<Integer> randomList = new ArrayList<>();
Random rand = new Random();

for (int i = 0; i < p.length; i++) {
    for (int i = 0; i < p[i].length; i++) {

            int randomNum = rand.nextInt((300 - 1) + 1) + 1;

            while (randomList.contains(randomNum)) {
                randomNum = rand.nextInt((300 - 1) + 1) + 1;
            }

            excludedNumbers.remove((Integer) randomNum); // the new line
            randomList.add(randomNum);
    }
} 

Integer 的演员确保List 使用Object 删除功能而不是int 删除选项。

【讨论】:

  • 这就是我要找的东西,但是我怎么能从列表中取出单个 int 项,然后将它们中的每一个添加到数据库表中呢?
  • @hiro228 像其他列表一样迭代列表?您很可能需要使用 JDBC 连接到数据库。不过,这超出了这个问题的范围,如果您在尝试时遇到问题,我建议您尝试一下并提出关于数据库部分的新问题。
  • 我已经建立了数据库,我的意思是我需要将这些单独的 int 项单独放入数据库中。甚至只是在控制台上单独打印它们而不是 [1, 4, 77, 299, 3...]。我是一个完整的java初学者
  • @hiro228 另外,我将第一个数组的边界更正为 1 到 300 而不是 0 到 299,这是之前循​​环中的小错误。以为我会让你知道。
【解决方案2】:

我希望你的问题是正确的,因为这对我来说有点难以理解。 我会这样做:

ArrayList<Integer> notIncludedNumbers = new ArrayList<Integer>();
for (int i = 0; i <= 300; i++){
   if (!list.contains(i))
      notIncludedNumbers.add(i);
}

您遍历从 1 到 300 的所有数字并将它们添加到 notIncludedNumbers 列表中,如果它们不包含在您的 20 个随机数列表中。 希望能帮上忙。

【讨论】:

    【解决方案3】:

    我认为最好的方法是生成一个单独的列表并用 1 到 300 之间的每个数字填充它。之后,只需遍历生成的随机数列表并从 1 到 300 中删除这些元素列表。像这样的..

    ArrayList<Integer> list300 = new ArrayList<Integer>();
    For (int i = 1; i <= 300; i++) {
        list300.add(i);
    }
    
    For (int i = 0; i < list.size(); i++) {
        if (list300.contains(list[i])) {
            list300.remove(Integer.vaueOf(list[i]));
        }
    }
    

    【讨论】:

      【解决方案4】:

      我花了很长时间才明白你的问题是什么。

          ArrayList<Integer> otherNumbers = new ArrayList<Integer>();
          for(int x = 0; x < list.size(); x++) {
              if(!list.contains(x+1)) {
                  otherNumbers.add(x+1);
              }
          }
      

      您可以通过跳过 20 数字列表包含的值来填充新列表。

      【讨论】:

        【解决方案5】:

        如果您确实需要两个列表,就像您说的那样,那么这里有一个方法:

        final int numberOfInts = 300;
        List<Integer> excluded = new ArrayList<>();
        for (int i = 1; i <= numberOfInts ; i++) {
            excluded.add(i);
        }
        
        Random rand = new Random();
        List<Integer> included = new ArrayList();
        for (int i = 0; i < p.length; i++) {
            included.add(excluded.remove(rand.nextInt(excluded.size())));
        }
        

        【讨论】:

          猜你喜欢
          • 2012-02-26
          • 1970-01-01
          • 1970-01-01
          • 2015-12-22
          • 1970-01-01
          • 2023-03-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多