0

I'm trying to do roshambo (or Rock-Paper-Scissors) and have the computer output Rock, Paper, or Scissors in place of a random value from 0 to 2. However, I'm not getting random values. When the program runs it goes through a while loop, and each time it goes through the loop, the value remains the same. However, if I stop, then re-run the application, the value changes, and remains that same value for the duration of the loop. Am I coding this method correctly?

public class Player2 extends Player{

    private Random rand;

    public Player2(){
        rand = new Random();
    }

    public Roshambo generateRoshambo() {
        int min = 0;
        int max = 2;
        int randomNum = rand.nextInt(max - min + 1) + min;

        if(randomNum == 0){
            return Roshambo.ROCK;
        } else if (randomNum == 1) {
            return Roshambo.PAPER;
        } else {
            return Roshambo.SCISSORS;
        }
    }
}

Here is the loop:

Roshambo value = p2.generateRoshambo(); 
String cont = "Yes";

do{

        System.out.print("\nRock, Paper, or Scissors: ");
        String choice = sc.nextLine();


        System.out.println("\n" + name + ": " + choice);
        System.out.println("Computer: " + value);

        if (value == ...)
        {
            System.out.println("\n" + name + " Wins!\n");
        } 

        else if (value == ...)
        {
            System.out.println("\nYou Tied!\n");
        }

        else 
        {
            System.out.println("\nThe Computer Wins!\n");
        }

        System.out.print("Play again? (Yes/No): ");
        cont = sc.nextLine();
    } while (cont.equalsIgnoreCase("Yes"));
4
  • 3
    Can you show the while loop? Commented Jan 8, 2013 at 23:06
  • 2
    first of, int randomNum = rand.nextInt(max); is the proper way Commented Jan 8, 2013 at 23:10
  • @DmitryKvochkin only in this specific example and it would still be rand.nextInt(max+1) since he needs 3 values. The -min +min part is a noop with 0 so it wont change a thing. Commented Jan 8, 2013 at 23:13
  • use rand.nextInt(3) to have 3 possible values. You can add +1 outside the brackets in order to get values from 1 to 3 instead of 0 to 2. Unless your min and max will be of use later on, you shouldn't make it more complicated than needed. Commented Jan 8, 2013 at 23:14

2 Answers 2

6

I just realized my own mistake. I called p2.generateRoshambo() outside of my do-while loop. By putting the following in my loop, I was able to solve the problem:

Roshambo value = p2.generateRoshambo()
Sign up to request clarification or add additional context in comments.

1 Comment

For these kind of questions its always good to make sure that your example code actually produces the bug, posting made up/untested code makes it harder to help.
1

The most likely cause of the symptom is creating a new Random instance each time round. The default seed is based on a clock that may not have ticked in a tight loop. It is important to create as few Random instances a possible, usually only one in a program, and go on getting values from it.

1 Comment

Unless he's creating a new Player2 every time, that's not the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.