简体   繁体   中英

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.

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).

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?

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 .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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