# Thread: Using loops for dice probability

1. Member Join Date
Oct 2014
Posts
2
Rep Power
0

## Using loops for dice probability

Hi, I'm new to posting here so please forgive me if this is a simple solution/ has already been answered.

Anyway, I'm taking AP Computer Science with a virtual school and I'm having trouble with this assignment. The instructions are to "write a program to simulate tossing a pair of 11-sided dice and determine the percentage of times each possible combination of the dice is rolled... then ask the user
to enter the number of sides on a die" The code is all provided here, I just have to put it in the right spots. (Side note, I cannot use arrays.) Here's the original file I was given to work with:

Java Code:
```import java.util.Random;
import java.util.Scanner;

public class DiceProbability
{
public static void main(String[] args)
{
//Declare and initialize variables and objects

//Input: ask user for number of rolls and number of sides on a die

//***************************************************************************************
//Using nested loops, cycle through the possible sums of the dice.
//Roll the dice the given number of times for each sum.
//Count how many times the sum of the dice match the current sum being looked for.
//***************************************************************************************
//Loop to increment through the possible sums of the dice
//Loop to throw dice given number of times
//Randomly generate values for two dice
//Check if the sum of dice is equal to the given sum
//After all throws, calculate percentage of throws that resulted in the given sum
//Print results

// Use the code below to complete the program. All statements are complete and when placed in the proper order, the program will work!
// Be sure to indent code properly and include documation for each section..
System.out.println("    " + sum + "\t\t" + probability );
System.out.print("Please enter the number of sides on a die: ");
System.out.print("Please enter the number of rolls: ");
System.out.print("\n Sum of dice \t Probability \n");
Scanner in = new Scanner(System.in);
Random randNum = new Random();
probability = (double)match / numRolls * 100;
numSides = in.nextInt();
numRolls = in.nextInt();
match++;
match = 0;
int sum = 0;
int numSides = 0;
int numRolls = 0;
int match = 0; //Number of times sum of dice matches the current sum
int die2 = 0;
int die1 = 0;
double probability = 0.0;
die2 = randNum.nextInt( numSides ) +1;
die1 = randNum.nextInt( numSides ) +1;

if ( die1 + die2 == sum )
{
} // end if

for( sum = 2; sum <= (numSides * 2); sum++ )
{
} //end for

for( int roll  = 0; roll < numRolls; roll++ )
{
} //end for

} //end main
}//end class DiceProbability```
So I worked with it for some time and came up with this:
Java Code:
```public class DiceProbability1
{
public static void main(String[] args)
{
//Declare and initialize variables and objects
int sum = 0;
int numSides = 0;
int numRolls = 0;
int match = 0; //Number of times sum of dice matches the current sum
int die2 = 0;
int die1 = 0;
double probability = 0.0;
Scanner in = new Scanner(System.in);

//Input: ask user for number of rolls and number of sides on a die
System.out.print("Please enter the number of sides on a die: ");
numSides = in.nextInt();
System.out.print("Please enter the number of rolls: ");
numRolls = in.nextInt();

System.out.print("\n Sum of dice \t Probability \n");

//***************************************************************************************
//Using nested loops, cycle through the possible sums of the dice.
//Roll the dice the given number of times for each sum.
//Count how many times the sum of the dice match the current sum being looked for.
//***************************************************************************************
//Loop to increment through the possible sums of the dice
//Loop to throw dice given number of times
//Randomly generate values for two dice
//Check if the sum of dice is equal to the given sum
//After all throws, calculate percentage of throws that resulted in the given sum
//Print results

Random randNum = new Random();

for( sum = 2; sum <= (numSides * 2); sum++ )
{
match = 0;
for( int roll  = 0; roll < numRolls; roll++ )
{
die1 = randNum.nextInt( numSides ) +1;
die2 = randNum.nextInt( numSides ) +1;
if ( die1 + die2 == sum )
{
match++;
} // end if
probability = (double)match / numRolls * 100;
System.out.println("    " + sum + "\t\t" + probability );
} //end for
} //end for

} //end main
}//end class DiceProbability```
When I run it, I get something like this (just a part of my output):
Please enter the number of sides on a die: 6
Please enter the number of rolls: 2

Sum of dice Probability
2 0.0
2 0.0
3 0.0
3 0.0
4 0.0
4 0.0
5 0.0
5 0.0
6 50.0
6 50.0
7 0.0
7 0.0

Now I don't really understand the concept in the first place as the lesson was extremely uninformative, so I don't know what went wrong here. Please help! (By the way, I'm using BlueJ, not sure if that's relevant.)  Reply With Quote

2. ## Re: Using loops for dice probability

You're printing the sum and the probability at each and every roll; you don't want that; you want to print those numbers after all rolls are done (hint: move lines #50 and \$51 after line #52).

kind regards,

Jos

ps. and increase the number of rolls for better accuracy.  Reply With Quote

3. Member Join Date
Oct 2014
Posts
2
Rep Power
0

## Re: Using loops for dice probability Originally Posted by JosAH You're printing the sum and the probability at each and every roll; you don't want that; you want to print those numbers after all rolls are done (hint: move lines #50 and \$51 after line #52).

kind regards,

Jos

ps. and increase the number of rolls for better accuracy.
Yes that's it, thank you so much!  Reply With Quote

#### Posting Permissions

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