Sigma
Sigma

Reputation: 13

No repeating random number in loop

I am beginner in Java, Now, I am trying to create the Guess Game in computerized player. I'm ASL fluent, Please forgive me if I make a poorly grammar in english. I will do my best to make this clear.

I am trying to stop a repeating random number from integer. I used arraylist to make a random from list, seem random still pick up the same value, I thought random won't have same value. I used Collections.shuffle, but it only works in array. I used arraylist contains, but it require to have at least one array to make a true or false, it can't determined the true and false when array is empty when java is run in beginning. Please anyone can help me to stop this repeat? Thank you in advance time. This "int a" is from other class file (it is a Math.random).

public class GameGuess extends Thread {

    int a;

    public void Send(int a) {

        this.a = a;

        ArrayList myArray = new ArrayList();
        ArrayList secArray = new ArrayList();

        Random rand = new Random();
        int p1;
        int p2;

        do {

            p1 = (rand.nextInt(60 - 50) + 50);
            p2 = (rand.nextInt(60 - 50) + 50);

            myArray.add(p1);
            secArray.add(p2);

            System.out.println("Player 1: " + p1);
            System.out.println("Player 2: " + p2);

            if (p1 == a && p2 == a) {
                System.out.println("Both Player1 and 2 are tied!");
                break;
            }
            if (p1 == a) {
                System.out.println("Player 1 wins!");
            }
            if (p2 == a) {
                System.out.println("Player 2 wins!");
            }
            if (p1 != a && p2 != a) {
                System.out.println("No one win, try again!");
            }

        } while (p1 != a && p2 != a);

        System.out.println("Player 1 picked: " + myArray);
        System.out.println("Player 2 picked: " + secArray);
        System.out.println("The secret number is " + a);

    }
}

Upvotes: 1

Views: 1621

Answers (3)

erickson
erickson

Reputation: 269627

Use a Set to reject random guesses that have already been tried. A Set will be a tiny bit more efficient than a List, but more importantly, it will make clear to anyone reading your code that each element in the collection can occur only once. This makes your code more clear.

Set<Integer> p1Guesses = new LinkedHashSet();
Set<Integer> p2Guesses = new LinkedHashSet();

int p1, p2;
do {
  p1 = rand.nextInt(10) + 50;
  while (!p1Guesses.add(p1))
    p1 = rand.nextInt(10) + 50;
  p2 = rand.nextInt(10) + 50;
  while (!p2Guesses.add(p2))
    p2 = rand.nextInt(10) + 50;
  ...

By using LinkedHashSet, the order of guesses will be recorded. The add() method of Set returns false if the added element already exists in the set, so that condition can control the loop.

Alternatively, you can add all the numbers in the range to a list, and shuffle it. But if you want to print the guesses that have been tried, it will change the logic a little bit. Essentially, you'd need to find the index of a in each shuffled list. Whichever is lower wins. You could print a sub-list of each string of guesses from zero to its respective correct index.

Upvotes: 1

Kristjan Veskim&#228;e
Kristjan Veskim&#228;e

Reputation: 992

You could generate a new random number if the player has already had that:

    ArrayList myArray = new ArrayList();
    ArrayList secArray = new ArrayList();

    Random rand = new Random();
    int p1;
    int p2;

    do {

        p1 = (rand.nextInt(60 - 50) + 50);

        while (myArray.contains(p1))
        {
            p1 = (rand.nextInt(60 - 50) + 50);
        }

        p2 = (rand.nextInt(60 - 50) + 50);

        while (secArray.contains(p2))
        {
            p2 = (rand.nextInt(60 - 50) + 50);
        }

        myArray.add(p1);
        secArray.add(p2);

Upvotes: 0

Vasyl Lyashkevych
Vasyl Lyashkevych

Reputation: 2222

You can upgrade you condition with limitation for iteration. For example,

    int maxGame = 100;
    int i = 0;

        do {

            p1 = (rand.nextInt(60 - 50) + 50);
            p2 = (rand.nextInt(60 - 50) + 50);
...

           i++;
        } while (i<maxGame && p1 != a && p2 != a);

Upvotes: 0

Related Questions