简体   繁体   English

生成两个相同的随机数,另一个不同

[英]Generating two identical random numbers and one that is different

I'm learning android development and trying to make a simple Monty Hall problem game. 我正在学习android开发,并试图制作一个简单的Monty Hall问题游戏。

Basically you have three doors to chose from, and one door has a car behind it, while each of the other two has a goat behind it. 基本上,您可以选择三扇门,其中一扇门后面有一辆汽车,而其他两扇门后面都有一只山羊。

I made a do while loop with a condition to make sure the three random numbers will not all be 0 (meaning goat) or will not have more than on variable with the value 1 (car). 我做了一个条件为do的循环,以确保三个随机数不会全部为0(意味着山羊),或者不会有大于on的值1(汽车)。

But when I run the program and go to this activity, it will be stuck in a black screen with no error as though it's in an infinite loop. 但是,当我运行该程序并转到此活动时,它将陷入黑屏,并且没有错误,就好像它处于无限循环中一样。

Is the logic in the do while loop correct? do while循环中的逻辑是否正确?

public class Game extends Activity{

    ImageView image1, image2, image3;

    int[] images={R.drawable.gaot1, R.drawable.eleanormustang};     
    Random r = new Random(); 

    int i1 = 0; 

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);      
        setContentView(R.layout.gameactivity);  
        picClicked();   
    }

    public void picClicked() {
        do {
            i1 = r.nextInt(2 - 0) + 0; 
            i2 = r.nextInt(2 - 0) + 0;
            i3 = r.nextInt(2 - 0) + 0;
        } while ((i1 & i2 &i3) ==0 || ( (i1 & i2) & (i1 & i3) & (i2 & i3) ) ==1 );

        image1 = (ImageView) findViewById(R.id.ImageView1);
        image2 = (ImageView) findViewById(R.id.ImageView2);
        image3 = (ImageView) findViewById(R.id.ImageView3);

        image1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //image1.setImageResource(R.drawable.gaot1);
                image1.setImageResource(images[i1]);
            }
        });

        image2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                image2.setImageResource(images[i2]);
            }
        });

        image3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                image3.setImageResource(images[i3]);
            }
        });
    }

}

You don't want to be generating three random numbers at all. 您根本不想生成三个随机数。 Think about it this way: you want one random number that indicates which door the prize is behind, and then you use that random number to generate i1 , i2 , i3 . 这样考虑:您需要一个随机数来指示奖品位于哪扇门之后,然后使用该随机数生成i1i2i3

So 所以

int door = r.nextInt(3);
i1 = (door==0 ? 1 : 0);
i2 = (door==1 ? 1 : 0);
i3 = (door==2 ? 1 : 0);

will do it without needing any kind of loop. 不需要任何循环就可以做到。

As chiastic-security answered, your approach is bad. 正如安全性所回答的那样,您的方法很糟糕。 However, it would have worked except you also made a logical error. 但是,它会起作用,除非您也犯了逻辑错误。 Your while condition checked whether the bitwise AND of i1, i2, and i3 is either 0 or 1. It always is when the numbers are 0 or 1. Instead, you might repeat the loop if the sum is not 1. 您的while条件检查i1,i2和i3的按位与是0还是1。数字始终为0或1时总是如此。相反,如果总和不为1,则可能重复循环。

do{
    ... 
} while ( i1 + i2 + i3 != 1 ); // so only (1,0,0), (0,1,0), or (0,0,1) passes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM