Java 如何避免在一行中两次生成相同的2个随机数

Java 如何避免在一行中两次生成相同的2个随机数,java,android,Java,Android,我试图使用计时器设置视图的背景色,每隔几秒钟更改一次颜色,但通常一行中会设置两次颜色,因为引用特定颜色的随机数会生成两次。如何更改代码以避免在一行中两次生成相同的随机数 final Runnable runnable = new Runnable() { @Override public void run() { Random rand = new Random(); int n = rand.nextInt(3); n +=

我试图使用计时器设置视图的背景色,每隔几秒钟更改一次颜色,但通常一行中会设置两次颜色,因为引用特定颜色的随机数会生成两次。如何更改代码以避免在一行中两次生成相同的随机数

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;

        }

    }
};
试试这个:

    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;

            }

        }
    };
Set generated=newhashset();
int-randomax=3;
final Runnable Runnable=新Runnable(){
@凌驾
公开募捐{
Random rand=新的Random();
int n=随机数(最大值);
while(generated.contains(n)&&generated.size()
唯一的方法是测试旧数字是否等于最新生成的数字

    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

您可以将上次创建的整数存储在变量“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;

        }

    }

你可以这样做

    //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;

        }

    }
};

很好,但是使用这个方法,生成的集合将包含所有的数字,然后呢?会发生什么?另外,问题只是想让两个随机数彼此分开,它并没有说它可以重复。@barotia我向作者展示了如何更改代码以避免连续两次生成相同的随机数。好的,现在让我们假设可运行部分将执行4次,作者以单独的代码发布,并提到它将在计时器中,输出是什么?@barotia你说得对。在这种情况下,它将是双重的。我编辑了答案。Thnx!不客气,但我还有一点要提,抛出异常是没有必要的,因为重复是允许的,但不能在彼此之后,1,2,1,3,1。。。仍然是一个有效的数字系列,同时检查另一个答案,它提供了一个更简单的解决方案,记住,亲吻,保持它愚蠢的简单。谢谢!我用一个列表跟踪数字,然后比较它们来运行它,但这更容易,真不敢相信我还没有想到这个解决方案