Question

Dice Rolling) Write an application to simulate the rolling of two dice. The application should use...

Dice Rolling) Write an application to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums.

Figure below shows the 36 possible combinations of the two dice. Your application should roll the dice 36,000 times. Use a one_dimensional array to keep track of the number of times each possible sum appears. Display the results in tabular format. Determine whether the totals are reasonable (e.g., there are six ways to roll a 7, so approximately one-sixth of the rolls should be 7).

Hints:

Keep track of how many times each total (2 through 12) occurs. This total is used to calculate the percentage of the time that each total occurs.

Define a loop that iterates 36,000 times. During each iteration, roll the dice, calculate the total and update the count for the particular total in the array.

Create an array large enough that you can use the sum of the dice as the index into the array.

Fig. below | The 36 possible outcomes of rolling two dice.

1

2

3

4

5

6

1

2

3

4

5

6

7

2

3

4

5

6

7

8

3

4

5

6

7

8

9

4

5

6

7

8

9

10

5

6

7

8

9

10

11

6

7

8

9

10

11

12

The Output Result

Sum   Frequency Percentage

2        1034        2.87

3        1987        5.52

4        3028        8.41

5        3881       10.78

6        4912       13.64

7        6135       17.04

8        5060       14.06

9        4032       11.20

10        2965        8.24

11        2003        5.56

12         963        2.68

Be sure your programs compile and run without error.

§ Include a comment at the beginning of each source file you submit that includes your name and the lab date.

§ Names for variables and other program components should be chosen to help convey the meaning of the variable.

Homework Answers

Answer #1

import java.util.Random;

public class HelloWorld
{
public static void main(String[] args)
{
Random r = new Random();
int[] totals = new int[36000];
int[] count = new int[11];
double[] per = new double[11];

for(int i = 0; i < 36000; i++)
totals[i] = Roll_Dice(r) + Roll_Dice(r);
  
for(int i = 0; i < 11; i++)
for(int j = 0; j < 36000; j++)
if(totals[j] == (i+2))
count[i]++;
for(int i = 0; i < 11; i++)
per[i]=((double)count[i]/360);
System.out.println("SUM\tFREQUENCY\tPERCENTAGE");
for(int i = 0; i < 11; i++)
{
System.out.print((i+2)+"\t"+ count[i]);
System.out.printf("\t\t%.2f\n", per[i]);
}
}
public static int Roll_Dice(Random r)
{
return(r.nextInt(6)+1);
}  
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
If a pair of dice is rolled, what is the probability of a) rolling a sum...
If a pair of dice is rolled, what is the probability of a) rolling a sum of 8, b) rolling a sum of 3 or rolling a sum of 6, c) rolling a sum of 4 or rolling doubles, if a pair of dice is rolled twice, whatnis the probability of d) rolling a sum of 9 on the first roll and rolling a sum of 2 on the second roll
In the rolling of two fair dice calculate the following: P(Sum of the two dice is...
In the rolling of two fair dice calculate the following: P(Sum of the two dice is 6)=  . P(Sum of the two dice is 10)=  . P(Sum of the two dice is not 9)=  . P(Sum of the two dice is 4 or 8)=  . P(Sum of the two dice is not 5 and not 7)=  .
Consider rolling two fair six-sided dice. a) Given that the roll resulted in sum of 8,...
Consider rolling two fair six-sided dice. a) Given that the roll resulted in sum of 8, find the conditional probability that first die roll is 6. b) Given that the roll resulted in sum of 4 or less, find the conditional probability that doubles are rolled. c) Given that the two dice land on different numbers, find the conditional probability that at least one die is a 6.
Question I: You roll 2 fair dice, one red and one green. a) What is the...
Question I: You roll 2 fair dice, one red and one green. a) What is the probability that the red die does land on 4? does not land on 4? b) What is the probability that either die lands on 4? that neither die lands on 4? c) What is the probability that the sum of the dice is 2? 3? 4? 5? 6? 7? 8? 9? 10? 11? 12?
Players A and B take turns at rolling two dice, starting with A. The first person...
Players A and B take turns at rolling two dice, starting with A. The first person to get a sum of at least 9 on a roll of the two dice wins the game. Find the probability that A will win the game if: (a) the game is just about to begin (b) 8, 4, 3, 7 and 7 have already been rolled (c) a draw is to be declared in the event of no-one winning within 6 rolls.
Players A and B take turns at rolling two dice, starting with A. The first person...
Players A and B take turns at rolling two dice, starting with A. The first person to get a sum of at least 9 on a roll of the two dice wins the game. Find the probability that A will win the game if: (a) the game is just about to begin (b) 8, 4, 3, 7 and 7 have already been rolled (c) a draw is to be declared in the event of no-one winning within 6 rolls.
You and a friend are rolling a set of 6 dice. The game works such that...
You and a friend are rolling a set of 6 dice. The game works such that if a die shows the values 1, 2, or 3 you will get a point for that die. Each die that shows 4, 5, or 6 your friend will get a point for. Construct a probability model for a single roll of the dice then answer the following. Step 1 of 5: What is the probability you made 2 points? Step 2 of 5:...
For the following questions, find the probability using a standard 6-sided die or two 6-sided dice....
For the following questions, find the probability using a standard 6-sided die or two 6-sided dice. Write your answer as a fraction or with a colon in lowest terms. Rolling a single die, what is the probability of rolling an even number? Rolling a single die, what is the probability of rolling a 5? Rolling a single die, what is the probability of rolling a 7? Rolling a single die, what is the probability of rolling a number less than...
You’re now going to do an experiment to find the relative probabilities of the sum of...
You’re now going to do an experiment to find the relative probabilities of the sum of 2 dice.      a. Roll the dice 96 times, record the sum for each roll in the first table below b. Organize your results from part a by filling in columns (2) and (3) in the table below. Sum of the dice Frequency (# of times sum occurred) Relative Probability of sum (# times/total) (use 3 decimals) Theoretical Probability (Copy from chart on p....
What is the total probability of rolling a natural on the come-out roll in craps? 2/36...
What is the total probability of rolling a natural on the come-out roll in craps? 2/36 4/36 6/36 8/36 If a player’s point in craps is 8, what is the probability that they will win by rolling another 8 before a 7? 5/36 6/36 5/11 6/11 Assume that rolling an 8 before a 7 will pay at 6-to-5 odds net. Find the net payoff on a $5 wager. $1.20 $5 $6 $11 Find the expected number of 5’s from 100...