samedi 19 novembre 2016

How to prevent a value from being generated in a random int? (Java)

I have two randomly generated variables. LoadG4 is outputted to one button and the other 3 buttons have a differing value for randoms1 generated. My aim here is to ensure that no value of the randomly generated randoms1 is equal to LoadG4. For example if the range is between 0 to 9 and LoadG4 ends up being 3, the other random numbers from randoms1.nextint... must not be 3. They could be 7, 4, or 5 for example, but not the same as LoadG4.

Here's my code:

Random GenerateG4 = new Random();
            int loadG4 = GenerateG4.nextInt(10);
            Random randoms1 = new Random();
            final TextView number = (TextView) findViewById(R.id.number);
            number.setText(""+loadG4);
            for(int allrbA=0; allrbA<4; allrbA++) {
                selectrb[allrbA].setText(""+randoms1.nextInt(10));
            }
            selectrb[rbselector].setText(""+loadG4);

How could I do this?

Many thanks in advance.




Aucun commentaire:

Enregistrer un commentaire