Results 1 to 3 of 3
  1. #1
    Sigmahokies is offline Member
    Join Date
    Apr 2017
    Location
    Richmond, VA
    Posts
    3
    Rep Power
    0

    Default From GuessGame thread, no repeating random number

    Hi everyone,

    I ma back from GuessGame thread, I fixed loop, I need to figure to make sure int in random doesn't do repeat number in loop. I tried to use array list to make random from list, but still repeating random. I used Collection.shuffle, but it doesn't work because it will works in array that exist inside. Even I used array list contains, but contains require to check at least one array inside, not empty in beginning in run in Java. So, anyone can help me?

    PHP Code:
    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);
    
        }
    }
    Thank you, everyone
    Last edited by Sigmahokies; 05-01-2017 at 06:15 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: From GuessGame thread, no repeating random number

    First, you can't use random and not have it repeat a number because then it wouldn't be random.

    I am not really certain what you are asking. If you want a shuffled array try initializing an ArrayList from 1 to n and the have Collections.shuffle() shuffle them. Then you can either use the ArrayList directly or convert it to an array using one of the toArray() methods.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: From GuessGame thread, no repeating random number

    You're using the shuffle() method wrong: populate an array or List with the numbers 0,1,2,3,4, ...n and then call the shuffle() method on it: it returns the same array with the numbers shuffled in it; if there were no duplicates in the list before the shuffle, there aren't any after the method has done its job ...

    kind regards,

    Jod
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Random number
    By _Chip in forum New To Java
    Replies: 4
    Last Post: 11-03-2012, 07:06 PM
  2. Replies: 4
    Last Post: 01-27-2012, 11:18 AM
  3. Replies: 7
    Last Post: 04-22-2011, 11:40 AM
  4. Non-Repeating Random Integers
    By Psyclone in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 09:04 PM
  5. GuessGame error
    By jmc724 in forum New To Java
    Replies: 2
    Last Post: 07-29-2008, 10:49 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •