HydratedDeer
HydratedDeer

Reputation: 43

Rolling Two Dice To Get a 7

I'm trying to get dice to roll until a 7 is obtained and then keep a running total of how many times it took to get a 7 through the trials. Once all trials have been completed, I need to average them. Right now I put in the number of trials but then it runs the first part of the 'for loop' ("Trial #1:") and then doesn't do anything else. What am I missing? This is being done in the dr.Java IDE.

***Edit* Semicolon after while loop was initial problem. Now I'm running into a different one. The loop produces the same output (shown here). Enter the seed value for the random number generator:

[1]

Enter the number of trials:

[10]

Trial #1: 10 rolls to roll a 7.

Trial #2: 10 rolls to roll a 7.

Trial #3: 10 rolls to roll a 7.

Trial #4: 10 rolls to roll a 7.

Trial #5: 10 rolls to roll a 7.

Trial #6: 10 rolls to roll a 7.

Trial #7: 10 rolls to roll a 7.

Trial #8: 10 rolls to roll a 7.

Trial #9: 10 rolls to roll a 7.

Trial #10: 10 rolls to roll a 7.

Average 1.0 rolls to roll a 7 over 10 trials.**

import java.util.*;

public class Geometric {
  public static void main(String[] args) {
    //create the Scanner and the Random number generator using a given seed
    Scanner scnr = new Scanner(System.in);
    System.out.println("Enter the seed value for the random number generator:");
    int seed = scnr.nextInt();
    Random r = new Random(seed);

    //TODO: complete the program
    System.out.println("Enter the number of trials:");
    int trials = scnr.nextInt();

    int sum = 0;
    int count = 0;
    int diceAdded = 0;
    for (int i=1;i<=trials;i++) {
      System.out.print("Trial #" + i + ": ");

    while (diceAdded != 7) {
        int dice = r.nextInt(6)+1;
        int dice2 = r.nextInt(6)+1;
        diceAdded = dice + dice2;
        count++;
    }
        System.out.println(count + " rolls to roll a 7.");
        sum = count;
      }
    double average = (sum/trials);
    System.out.println("Average " + average + " rolls to roll a 7 over " + trials + " trials.");
  }
}

Upvotes: 0

Views: 677

Answers (2)

Nexevis
Nexevis

Reputation: 4667

Besides the typo you made for the semicolon already pointed out, you do not reset your variables inside of your for loop, so each iteration will execute on the same values every single time.

Fix this by moving your count and diceAdded variables into the for loop:

System.out.println("Enter the number of trials:");
int trials = scnr.nextInt();

int sum = 0;

for (int i=1;i<=trials;i++) {
  System.out.print("Trial #" + i + ": ");
  int count = 0;  
  int diceAdded = 0;
  while (diceAdded != 7) {
    int dice = r.nextInt(6)+1;
    int dice2 = r.nextInt(6)+1;
    diceAdded = dice + dice2;
    count++;
  }
  System.out.println(count + " rolls to roll a 7.");
  sum += count;
}

In addition, note that I changed sum = count to sum += count, which will add the counts all together, instead of resetting the value to previous iteration's value of count.

Thanks for @jrook for pointing out your average function is operating on integer division. You can fix that by casting one of the values to double like so: double average = ((double) sum / trials);. So now not only whole numbers are outputted.

Output:

Enter the seed value for the random number generator:
121
Enter the number of trials:
5
Trial #1: 15 rolls to roll a 7.
Trial #2: 19 rolls to roll a 7.
Trial #3: 9 rolls to roll a 7.
Trial #4: 2 rolls to roll a 7.
Trial #5: 2 rolls to roll a 7.
Average 9.4 rolls to roll a 7 over 5 trials.

Upvotes: 2

Kosonome
Kosonome

Reputation: 470

The main problem of stopping in "Trial #1:", is that your while is stopping right away because the semicolon.

Change while (diceAdded != 7); { to while (diceAdded != 7) {.

Upvotes: 0

Related Questions