0

![Screebshot][1]]

Is there anyway I can have a different number generate as x, between 0 and 3, every time i call the gamePlay() method below? The gameplay method is on a loop and I want it so that every time the method is called a new value for x between 0 and 3 is created. I tried a lot of things such as the random generator in the code below but I cant figure it out.

public class MainActivity extends AppCompatActivity {

    final Handler h = new Handler();
    RelativeLayout rLayout;
    Button play, retry;
    TextView title, lose, score;
    ImageView rc, gc, bc, yc, tyc, trc, tgc, tbc,tblc;
    int gscore = 0;




    protected void gamePlay(){

        x = r.nextInt(4);

        if(x == 0){
            tbc.setVisibility(View.VISIBLE);
        }
        else if(x == 1){
            tgc.setVisibility(View.VISIBLE);
        }
        else if(x == 2){
            trc.setVisibility(View.VISIBLE);
        }
        else if(x == 3){
            tyc.setVisibility(View.VISIBLE);
        }
        rLayout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                if(x == 0){

                    if(tbc.getY() < bc.getY()+50 && tbc.getY() > bc.getY()-50){
                        gscore = gscore+1;
                        score.setText(String.valueOf(gscore +""));
                        tgc.setY(tblc.getY());
                        tbc.setY(tblc.getY());
                        trc.setY(tblc.getY());
                        tyc.setY(tblc.getY());
                    }
                    else{
                        gameLost();
                    }
                }
                else if(x==1)
                    if(tbc.getY() < gc.getY()+50 && tbc.getY() > gc.getY()-50){
                        gscore = gscore+1;
                        score.setText(String.valueOf(gscore+"" ));
                        tgc.setY(tblc.getY());
                        tbc.setY(tblc.getY());
                        trc.setY(tblc.getY());
                        tyc.setY(tblc.getY());
                    }
                    else{
                        gameLost();

                    }
                else if(x==2)
                    if(tbc.getY() < rc.getY()+50 && tbc.getY() > rc.getY()-50){
                        gscore = gscore+1;
                        score.setText(String.valueOf(gscore +""));
                        tgc.setY(tblc.getY());
                        tbc.setY(tblc.getY());
                        trc.setY(tblc.getY());
                        tyc.setY(tblc.getY());


                    }
                    else{
                        gameLost();

                    }
                else if(x==3)
                    if(tbc.getY() < yc.getY()+50 && tbc.getY() > yc.getY()-50){
                        gscore = gscore+1;
                        score.setText(String.valueOf(gscore +""));
                        tgc.setY(tblc.getY());
                        tbc.setY(tblc.getY());
                        trc.setY(tblc.getY());
                        tyc.setY(tblc.getY());


                    }
                    else{
                        gameLost();
                    }
            }
        });
    }

2 Answers2

0

The problem is that you are creating a new random number generator with the same seed each time that you call gamePlay(). That will give you the same random number sequence each time. Guaranteed.

Make the variable holding the random number generator reference an instance variable, and initialize it once.

Also, Random.nextInt(4) will return 0, 1, 2 or 3. I don't know if that's what you mean by "between 0 and 4".

I recommend that you take the time to read the javadocs for Random.


Someone suggested that you could remove the seed argument to the Random constructor. However, that would mean that you get a random seed from the OS each time. That is relatively expensive, and (in some contexts) can cause problems by depleting the OS's source of entropy.

This may be OK in an Android AP where the calls are triggered by user events. But it is not a good idea.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

If you want to get random number between 0 and 4 then change the argument to 5.

Because nextInt()

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)

int x = r.nextInt(5);

Also drop the final keyword because you want your value to be changed even after it's allocated.

If it's giving you error, then make it a field variable and don't make it final. that should solve your problem.

    ...
    }
});}


Random r = new Random(seed+gscore); 
//initialize it in constructor below `seed` and `gscore` variables;

private int x; //move the declaration to class instead of method
protected void gamePlay(){
    x = r.nextInt(5);
    ...

Also You need to set seed only once. If you always set the seed then it will keep giving you same answer.

See this answer


EDIT

You can also switch to Math.random() to generate random number.

x = (int)(Math.random()*10)%5;
Community
  • 1
  • 1
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
  • look at how i edited the code based on the suggestions but still it generates the same number i updated the origional post and also i want numbers 0,1,2,3. –  Mar 05 '17 at 05:10
  • @z123 see edited answer. You need to set seed only once. If you set it everytime, then it will keep giving you same answer. – Raman Sahasi Mar 05 '17 at 05:18
  • i think i got what you are saying i edited my code on the post see if it is what you said. I declared both r and x as instance variables in the class outside methods and initialized them both in the gamePlay method. –  Mar 05 '17 at 05:23
  • @z123 please move `r = new Random();` also outside method. If you keep initializing it again and again each time your method is called, then it will keep giving you same results. – Raman Sahasi Mar 05 '17 at 05:25
  • no it still didnt work. i posted my whole code so you can look at it i will also post a screenshot of the game check it out. –  Mar 05 '17 at 05:27
  • you found the problem? –  Mar 05 '17 at 05:34
  • @You're still getting same random int? – Raman Sahasi Mar 05 '17 at 05:36
  • yea still getting same –  Mar 05 '17 at 05:36
  • @z123 can you switch to `Math.random()` to get random number? see edit. – Raman Sahasi Mar 05 '17 at 05:38
  • I think that random number is being generated correctly, there might be some problem in logic. Can you debug your application and tell if number is random or not each time. Don't rely on output. – Raman Sahasi Mar 05 '17 at 05:43
  • it keeps giving me x =0; –  Mar 05 '17 at 05:54