1

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

    }
}
brso05
  • 13,142
  • 2
  • 21
  • 40
Sigma
  • 21
  • 8
  • 2
    *"I thought random won't have same value"* - random does not mean non-repeating (which already breaks the definition of random) and in code you always have **pseudo**-random number generators. – UnholySheep May 01 '17 at 17:12
  • 1
    Before adding a number to array you can use .contains to see if it's either array list already and reroll if it is. – Alex May 01 '17 at 17:12
  • Perhaps you could use a TreeSet (so you only have unique numbers, in sorted order), and check if the Set contains the number you generated. if it does, try again. Else, use the number. – Ishnark May 01 '17 at 17:14
  • *Collections.shuffle, but it only works in array*: no. Collections.shuffle() takes a List as argument. https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#shuffle-java.util.List-. What's the problem with shuffle()? And why, 12 years after the release of Java 5, are you still using raw ArrayLists instead of generic ArrayList? – JB Nizet May 01 '17 at 17:14
  • Possible duplicate of [Creating random numbers with no duplicates](http://stackoverflow.com/questions/4040001/creating-random-numbers-with-no-duplicates) – bhspencer May 01 '17 at 17:19
  • please try searching before posting a new question. This has been asked many times before. – bhspencer May 01 '17 at 17:19
  • `ArrayList myArray = new ArrayList();` : Just a tip ; You can specify the values the lists are going to contain : `List myArray=new ArrayList();` – Luatic May 01 '17 at 17:21

3 Answers3

1

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.

erickson
  • 265,237
  • 58
  • 395
  • 493
0

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);
Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38
0

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);
  • Wow! it works! Thank you so much!!! I am very appreciate!!! I promise you i will pay the forward to anyone who need help! – Sigma May 01 '17 at 17:25