Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    258
    Thanks
    1
    Thanked 2 Times in 2 Posts

    java: Not so random

    Well, I finished up a Paper-Scissor-Rock game on java. It starts off with 16 players, then goes to 8, then 4, then 2. The problem is that in nine out of ten games, the winners are the same players.
    And the problem also seems to extend to even when I(human) play agaisnt a computer. 90% of the time I lose, no matter what my choice is
    Here's a bit of my code, but I don't think there's anything wrong with it:
    Code:
    //WinnersR1[] is an array were Round1 winners are stored
    //Players[0] is human user
    //Players[] is an array of strings with Players names
    
    boolean win = false;
                while (win == false)
                {
                   System.out.print("*");        
                   int computer = rand.nextInt(3);//makes computer choice: 1-Scissor, 2-Paper, 3-Rock
                   UserChoice = scan.nextLine();
                
                
                //************************************************
                
                   if (UserChoice.equalsIgnoreCase("s")) //if user chooses scissors
                   {
                   
                      if (computer == 1){
                         System.out.println("*Computer Chooses Scissor");
                         System.out.println("*Tie, Game will be Replayed. Choose Paper(P), Scissors(S), or Rock(R):");
                      }
                      
                      else if (computer == 2)
                      {
                         System.out.println("*Computer Chooses Paper");
                         System.out.println("*" + Players[0] + " cuts " + Players[1]);
                         WinnersR1[0] = Players[0];
                         LosersR1[0] = Players[1];
                         win = true;
                         
                      }
                      
                      else
                      {
                         System.out.println("*Computer Chooses Rock");
                         System.out.println("*You loose");
                         WinnersR1[0] = Players[1];
                         LosersR1[0] = Players[0];
                         win = true;
                      }
                   }
    That's just for the Scissors part of it, but the rest looks pretty much like it.

    To give a better description of the problem, lets say I play 10 times, and every time I choose Scissor. 9 out of those 10 times the computer will choose Rock. Something similar happens if I choose Paper or Rock...
    Any idea what could be wrong?
    Last edited by daniel_g; 05-10-2006 at 07:41 AM.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    The rand function as I recall from my days in college having to write games for our projects is that it is not all that random when it has few choices to choose from. About all you could do is try to use the rand method and incorporate some more logic with it to cause the choice to be more random.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

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