【问题标题】:Non repeating random integers in Java [duplicate]Java中的非重复随机整数[重复]
【发布时间】:2014-09-10 01:05:09
【问题描述】:

我试图让一段简单的 java 代码以随机顺序吐出数字 1-6 而不重复。我已经设法让它在 1 到 6 之间吐出六个随机整数,但它只检查一个数字是否被使用过一次。代码如下:

import java.util.Random;

public class kirbyInt

{

    public static void main(String[] args)
    {
        int num1, num2, num3, num4, num5, num6;


        Random rand = new Random();

        num1 = rand.nextInt((6-1) + 1) + 1;
        System.out.println(num1);

        num2 = rand.nextInt((6-1) + 1) + 1;

        if (num2 == num1)
        {
            num2 = rand.nextInt((6-1) + 1) + 1;
            System.out.println(num2);
        }
        else
        {
            System.out.println(num2);
        }

        num3 = rand.nextInt((6-1) + 1) + 1;

        if (num3 == num1 || num3 == num2)
        {
            num3 = rand.nextInt((6-1) + 1) + 1;
            System.out.println(num3);
        }
        else
        {
            System.out.println(num3);
        } 

        num4 = rand.nextInt((6-1) + 1) + 1;

        if (num4 == num1 || num4 == num2 || num4 == num3)
        {
            num4 = rand.nextInt((6-1) + 1) + 1;
            System.out.println(num4);
        }
        else
        {
            System.out.println(num4);
        } 

        num5 = rand.nextInt((6-1) + 1) + 1;
        if (num5 == num1 || num5 == num2 || num5 == num3 || num5 == num4)
        {
            num5 = rand.nextInt((6-1) + 1) + 1;
            System.out.println(num5);
        }
        else
        {
            System.out.println(num5);
        } 

        num6 = rand.nextInt((6-1) + 1) + 1;
        if (num6 == num1 || num6 == num2 || num6 == num3 || num6 == num4 || num6 == num5)
        {
            num6 = rand.nextInt((6-1) + 1) + 1;
            System.out.println(num6);
        }
        else
        {
            System.out.println(num6);
        } 

    }

}

【问题讨论】:

  • 您可以使用 do-while 循环而不是 if 来生成一个新数字,直到它符合您的条件。但是,您确实需要考虑为此使用数组或集合。
  • 不能用数组什么的吗?当你看到太多重复的代码时,你可能可以简化你的问题,就是这样。
  • 可能duplicate.

标签: java random integer


【解决方案1】:

你可以把你的数字放在一个 ArrayList 中。然后获取 0-5 之间的随机数并打印,然后从 ArrayList 中删除该位置的数字。这将只留下 5 个数字,获取 0-4 之间的下一个随机数,打印并删除该随机索引处的数字,依此类推,直到你只有一个,然后打印那个。

【讨论】:

    【解决方案2】:

    有一种更简单的方法可以解决这个问题。让我们准确地重申你想要做什么。

    问题

    您想创建n 随机数,其中给定数字k 不在之前的k-1 数字中

    解决方案

    创建一个Set 用于保存随机数,当您创建一个新的时,当且仅当它不在Set 中时才将其添加到集合中,否则生成一个新的随机数并重复。

    代码示例

    import java.lang.String;
    import java.util.Random;
    import java.util.Set;
    import java.util.HashSet;
    
    public class RandomNum {
    
        public static final void main(String[] args){
            final Random r = new Random();
            final Set<Integer> s = new HashSet<>();
            for(int i = 0; i < 6; i++){
                    while(true) {
                    int num = r.nextInt(6) + 1;
                    if (s.contains(num) == false) {
                        s.add(num);
                        System.out.println(num);
                        break;
                    }
                }
            }
        }
    }
    

    输出

    /tmp$ javac RandomNum.java
    /tmp$ java RandomNum
    5
    1
    3 
    2
    4
    6
    

    其他选项

    原则上您可以使用其他数据结构来保存生成的随机数,例如ListSet 有一个很好的优势,即在您想要生成超过 6 个值的情况下,搜索速度相对较快。您可以使用ArrayList 获得类似的性能。然而,使用Set 有一个更重要的优势,它反映了您想要的语义(一组无序的非重复值)。您不关心集合中的顺序,并且您不希望重复值List 是有序的值集合,Set 是无序的值集合(这意味着元素不能在 Set 中重复,并且它们彼此之间没有任何关系)添加到Set)。在编码时,几乎总是选择不仅工作而且反映你的意思的语义的数据结构是一个好主意。

    【讨论】:

      【解决方案3】:

      小问题:你为什么用rand.nextInt((6-1) + 1)而不是rand.nextInt(6)

      现在,关于手头的任务:我会从可能的数字列表开始;随机选择列表中的现有索引之一(结果是该索引处的数字);从列表中删除号码。列表为空时,不能再取号码。

      该解决方案的好处是,您不必多次重试生成随机数,直到找到尚未使用的随机数。

      示例代码:

      import java.util.ArrayList;
      import java.util.List;
      import java.util.Random;
      
      public class Main {
      
          public static void main(String[] args) {
              final List<Integer> integers = new ArrayList<>();
              final Random rand = new Random();
      
              // Fill list
              for (int i = 1; i <= 6; i++) {
                  integers.add(i);
              }
      
              // Take numbers in random order
              while (!integers.isEmpty()) {
                  final int index;
                  if (integers.size() > 1) {
                      index = rand.nextInt(integers.size() - 1);
                  } else {
                      // rand.nextInt(0) would throw an exception rather than return 0
                      index = 0;
                  }
                  final int result = integers.remove(index);
                  System.out.println("Result: " + result);
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        你可以这样做

        public class NonRepreatedRandomNumbers {
        
            public static void main(String[] args) {
                ArrayList myList = new ArrayList();
                Random rd = new Random();
                int num;
                while(true){
                    num = rd.nextInt(6) + 1;
                    if(myList.size() ==  6)
                        break;
                    if(!myList.contains(num)){
                        myList.add(num);
                        System.out.println(myList);
                    }else
                        continue;
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2014-12-06
          • 2014-02-12
          • 2013-12-24
          • 2013-04-06
          • 1970-01-01
          相关资源
          最近更新 更多