【问题标题】:How to avoid generating the same 2 random numbers twice in a row [duplicate]如何避免连续两次生成相同的2个随机数[重复]
【发布时间】:2019-09-23 10:04:18
【问题描述】:

我正在尝试使用计时器设置视图的背景颜色以每隔几秒更改一次颜色,但由于生成了引用特定颜色的随机数,因此通常会连续两次设置颜色两次。如何更改代码以避免连续两次生成相同的随机数?

final Runnable runnable = new Runnable() {

    @Override
    public void run() {

        Random rand = new Random();
        int n = rand.nextInt(3);
        n += 1;


        switch (n){

            case 1:

                colorView.setBackgroundColor(Color.GREEN);
                break;

            case 2:

                colorView.setBackgroundColor(Color.MAGENTA);
                break;

            case 3:

                colorView.setBackgroundColor(Color.CYAN);
                break;

        }

    }
};

【问题讨论】:

    标签: java android


    【解决方案1】:

    唯一的方法是测试旧数是否等于最新生成的数。

        Random rand = new Random();
        int n = rand.nextInt(3);
        n += 1;
        while (n == oldValue) {
            n = rand.nextInt(3);
            n += 1;
        }
        switch (n){
        ...
        }
        oldValue = n; //The oldValue should be global
    

    【讨论】:

    • 谢谢!我通过使用列表跟踪数字然后比较它们来运行它,但这更容易,不敢相信我没有这么早想到解决方案
    【解决方案2】:

    试试这个:

        Set<Integer> generated = new HashSet<>();
        int randomMax = 3;
        final Runnable runnable = new Runnable() {
    
            @Override
            public void run() {
    
                Random rand = new Random();
                int n = rand.nextInt(randomMax);
                while (generated.contains(n) && generated.size() < randomMax) {
                    n = rand.nextInt(randomMax);
                }
                if (generated.size() == randomMax) {
                    throw new Exception("all numbers already generated")
                }
                generated.add(n);
                n += 1;
                switch (n){
    
                    case 1:
    
                        colorView.setBackgroundColor(Color.GREEN);
                        break;
    
                    case 2:
    
                        colorView.setBackgroundColor(Color.MAGENTA);
                        break;
    
                    case 3:
    
                        colorView.setBackgroundColor(Color.CYAN);
                        break;
    
                }
    
            }
        };
    

    【讨论】:

    • 差不多好了,但是用这种方法,生成的集合会包含所有的数字,之后呢?会发生什么?另外,这个问题只是想把两个分开,根本没有说可以重复。
    • @barotia 我向作者展示了如何更改代码以避免连续两次生成相同的随机数。
    • 好的,现在假设runnable部分将被执行4次,作者作为单独的代码发布并提到它将在一个计时器中,输出会是什么?
    • @barotia 你是对的。在这种情况下它将是重复的。我编辑了答案。谢谢!
    • 不客气,但我还有一点要提,抛出异常是没有必要的,因为允许重复,但不能一个接一个,1,2,1,3,1...仍然是一个有效的数字系列,还要检查另一个答案,它提供了一个更简单的解决方案,记住,KISS,保持简单。
    【解决方案3】:

    您可以将上次创建的整数存储在变量“temp”中,并将其与当前步骤中创建的整数进行比较:

    final Runnable runnable = new Runnable() {
    
        @Override
        public void run() {
            if(tempInt == null){
                int tempInt = 0;
                int n = 0;
            }
            Random rand = new Random();
            while(n == tempInt){
                n = rand.nextInt(3);
                n += 1;
            }
            tempInt = n;
            switch (n){
    
                case 1:
    
                    colorView.setBackgroundColor(Color.GREEN);
                    break;
    
                case 2:
    
                    colorView.setBackgroundColor(Color.MAGENTA);
                    break;
    
                case 3:
    
                    colorView.setBackgroundColor(Color.CYAN);
                    break;
    
            }
    
        }
    

    【讨论】:

      【解决方案4】:

      你可以这样做

          //set it as a global variable
      
          int previousRandomNumber=0; //set it as 0 for first time
      
          final Runnable runnable = new Runnable() {
      
          @Override
          public void run() {
              int currentRandomNumber=new Random.nextInt(3);
              currentRandomNumber+=1;
      
              while(currentRandomNumber == previousRandomNumber){
              currentRandomNumber = new Random.nextInt(3);
              currentRandomNumber+=1;
              }
      
              /*store the current number as the previous number to check with next the generated number is the same or not*/
      
              previousRandomNumber=currentRandomNumber;
      
              switch (n){
      
                  case 1:
      
                      colorView.setBackgroundColor(Color.GREEN);
                      break;
      
                  case 2:
      
                      colorView.setBackgroundColor(Color.MAGENTA);
                      break;
      
                  case 3:
      
                      colorView.setBackgroundColor(Color.CYAN);
                      break;
      
              }
      
          }
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-29
        • 2016-02-16
        • 2017-12-26
        • 1970-01-01
        • 2020-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多