Dice Rolling Probability - How Do I Start It Off?

Just a quick question on how to start off a code on this. The program is supposed to simulate tossing a pair of 2-sided dice and determining the percentage of times each possible combination of the dice is rolled. The actual assignment is using 11-sided dice but I just want a simple code to start off with. I want to make it into one loop using for, if, and nested loops. How would a code like this look?

__Things I Need__

Loop to increment through the possible sums of the dice

Code :

for (int sum = 2; sum <= 4; sum++)

Loop to throw dice given number of times

Code :

for (int roll = 0; roll < totalRolls; roll++)

Randomly generate values for two dice

Code :

die1 = (int)(Math.random() * 10);
die2 = (int)(Math.random() * 10);
diceSum = die1 + die2;

Check if the sum of dice is equal to the given sum

Re: Dice Rolling Probability - How Do I Start It Off?

Not sure what the first loop (for(sum)) is supposed to do. Can you explain why sum starts at 2 and goes to 4?

Quote:

determining the percentage of times each possible combination of the dice is rolled.

That sounds like the dice should be rolled 1000s of times, each diceSum counted and then the % computed.

Say there are 3 different sums, and the dice were rolled 1000 times then the sums could be:

250, 500, 250

and the %s would be:

25, 50, 25

Re: Dice Rolling Probability - How Do I Start It Off?

Quote:

Originally Posted by

**Norm**
Not sure what the first loop (for(sum)) is supposed to do. Can you explain why sum starts at 2 and goes to 4?

That sounds like the dice should be rolled 1000s of times, each diceSum counted and then the % computed.

Say there are 3 different sums, and the dice were rolled 1000 times then the sums could be:

250, 500, 250

and the %s would be:

25, 50, 25

It's supposed to represent all the possible sums I could get from the dice. So the lowest possible number would be 2 and the highest would be 4.

If the sum on the dice is equal to the sum, then it raises by one. If not, it just goes on to the next sum and it loops for how many times the user asks for it to be rolled.

Re: Dice Rolling Probability - How Do I Start It Off?

Is the problem: What % of rolls produces a value given by the user? The number of rolls to make is also given by the user.

Re: Dice Rolling Probability - How Do I Start It Off?

Quote:

Originally Posted by

**Norm**
Is the problem: What % of rolls produces a value given by the user? The number of rolls to make is also given by the user.

It's similar to what you said. I need to display the probability of how many times a combination will occur depending on how many rolls the user input.

For example, since I'm doing a 2-sided dice and let's say the user enters in 1 roll:

2s: 25%

3s: 50%

4s: 25%

Re: Dice Rolling Probability - How Do I Start It Off?

Quote:

let's say the user enters in 1 roll:

Not sure I understand what you mean by 1 roll.

If a 2 is rolled then a 2 is rolled 1/1 = 100% of the time.

Re: Dice Rolling Probability - How Do I Start It Off?

Quote:

Originally Posted by

**Norm**
Not sure I understand what you mean by 1 roll.

If a 2 is rolled then a 2 is rolled 1/1 = 100% of the time.

I meant the user would enter in 1 as the number of rolls they want. Basically, it's like this:

-Ask the user to input how many times the dice will be rolled.

-Calculate the probability of each combination of dice.

I'm confused as how to do that. The snippets I put above are just thoughts but I'm not sure if they will actually be used.

Re: Dice Rolling Probability - How Do I Start It Off?

Quote:

I meant the user would enter in 1 as the number of rolls they want

If there is only one roll, how do you compute the %? Normally there are 1000s of rolls.

Quote:

Calculate the probability of each combination of dice.

Before you said the user would chose which sum of the dice the % was to be computed for.

I don't think you understand the problem yet.

Re: Dice Rolling Probability - How Do I Start It Off?

Quote:

Originally Posted by

**Norm**
If there is only one roll, how do you compute the %? Normally there are 1000s of rolls.

I don't think you understand the problem yet.

Yeah, that was a bad example. If it was only 1 roll, then it would be 100% for one combination. I think 100 rolls would be better. So, how would I code this problem?

Re: Dice Rolling Probability - How Do I Start It Off?

Before coding anything make a list of the steps (a design) the program should do to solve the problem.

When you have a design then work on coding it.

Re: Dice Rolling Probability - How Do I Start It Off?

Okay well here's what I have so far. I'm not sure if even this is correct. How do I display the number of times it matches for each sum?

Code :

import java.util.Random;
import java.util.Scanner;
public class DiceProbability
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Random randNum = new Random();
int match = 0; //Number of times sum of dice matches the current sum
int die1, die2;
int totalRolls = 0;
int diceSum = 0;
System.out.print("How many times do you want to roll the dice? ");
totalRolls = in.nextInt();
//Print heading for output table
System.out.println("Sum of Dice Probability");
for (int sum = 2; sum <= 22; sum++)
{
for (int roll = 0; roll < totalRolls; roll++)
{
die1 = randNum.nextInt(11);
die2 = randNum.nextInt(11);
diceSum = die1 + die2;
}
if (diceSum == sum)
{
match++;
}
else
{
}
}
//After all throws, calculate percentage of throws that resulted
//in the given sum.
//Print results
System.out.println(" 2s: " + match);
System.out.println(" 3s: ");
System.out.println(" 4s: ");
System.out.println(" 5s: ");
System.out.println(" 6s: ");
System.out.println(" 7s: ");
System.out.println(" 8s: ");
System.out.println(" 9s: ");
System.out.println(" 10s: ");
System.out.println(" 11s: ");
System.out.println(" 12s: ");
System.out.println(" 13s: ");
System.out.println(" 14s: ");
System.out.println(" 15s: ");
System.out.println(" 16s: ");
System.out.println(" 17s: ");
System.out.println(" 18s: ");
System.out.println(" 19s: ");
System.out.println(" 20s: ");
System.out.println(" 21s: ");
System.out.println(" 22s: ");
} //end main
}//end class DiceProbability

Re: Dice Rolling Probability - How Do I Start It Off?

Quote:

How do I display the number of times it matches for each sum?

Do the summing using an array and then you can print the contents of the array.

Did you ever design the program (make a list of the steps)?

What are the steps the posted code does to solve the problem?