Wrangler YJ
Wrangler YJ

Reputation: 9

Want to generate random numbers. simulating rolling dice

I am trying to load two arrays. representing the possible outcomes (1-6) of rolling dice. The first array gets loaded with the first dice outcome and the second array.....I am struggling with keeping both arrays in one loop that manages the amount of times the dice are rolled, and not generating the duplicate results. Believe i need to either re-seed between each roll, any help would be much appreciated.

Current output: Dice one: 5 Dice two: 5 Dice one: 3 Dice two: 3 Dice one: 1 Dice two: 1 Dice one: 6 Dice two: 6 Dice one: 6 Dice two: 6 Dice one: 2 Dice two: 2 Dice one: 5 Dice two: 5 Dice one: 2 Dice two: 2 Dice one: 1 Dice two: 1 Dice one: 1 Dice two: 1 second roll always matches the first

I have written as follows:

int min = 1;
    int max = 6;
    for (int i = 0; i < 10; i++)
    {
        Random rand;
        rand = new Random();

        int randomNumber = rand.nextInt(max) + min;
        diceOne[i] = randomNumber;
        System.out.print("Dice one: ");
        System.out.print(diceOne[i] + " ");


        int randomNumber2 = rand.nextInt(max) + min;

        diceTwo[i] = randomNumber2;
        System.out.print("Dice two: ");
        System.out.print(diceOne[i]);
        System.out.println();

Upvotes: 0

Views: 1493

Answers (2)

Snippet
Snippet

Reputation: 1560

diceTwo is never use. try code below

 int randomNumber = rand.nextInt(max) + min;
            diceOne[i] = randomNumber;
            System.out.print("Dice one: ");
            System.out.print(diceOne[i] + " ");


            int randomNumber2 = rand.nextInt(max) + min;

            diceTwo[i] = randomNumber2;
            System.out.print("Dice two: ");
            System.out.print(diceTwo[i]); //System.out.print(diceOne[i]);
            System.out.println();

Upvotes: 1

Tom Offermann
Tom Offermann

Reputation: 1421

Well, it looks like you are printing out diceOne[i] both times.

Upvotes: 0

Related Questions