Question: JAVA Introduction This is a simulation of rolling dice. We will roll 10,000 times in our program. The theoretical probability of rolling doubles of a
JAVA
Introduction
This is a simulation of rolling dice. We will roll 10,000 times in our program. The theoretical
probability of rolling doubles of a specific number is 1 out of 36 or approximately 278 out of
10,000 times that you roll the pair of dice. Since this is a simulation, the numbers will vary a
little each time you run it. We will start with a while loop, then use the same program, changing
the while loop to a do-while loop, and then a for loop.
Task #1 Loops
1. Copy the file DiceSimulation.java into NetBeans or other Java IDE tools.
2. The double roll of 1s and 2s are given in the code.
3. You follow the style to code for double roll value for 3 6 so all the doubles should be
around 278.
4. Change the while loop to do-while loop and run the program. Result should be similar.
5. Change the while loop to a for loop and run the program. Result should be similar.
This is what I have so far. Not very sure how to write the do-while loop or the for loop...
/**
This class simulates rolling a pair of dice 10,000 times and
counts the number of times doubles of are rolled for each different
pair of doubles.
*/
import java.util.Random; //to use the random number generator
public class DiceSimulation
{
public static void main(String[] args)
{
final int NUMBER = 10000; //the number of times to roll the dice
//a random number generator used in simulating rolling a dice
Random generator = new Random();
int die1Value; // number of spots on the first die
int die2Value; // number of spots on the second die
int count = 0; // number of times the dice were rolled
int snakeEyes = 0; // number of times snake eyes is rolled
int twos = 0; // number of times double two is rolled
int threes = 0; // number of times double three is rolled
int fours = 0; // number of times double four is rolled
int fives = 0; // number of times double five is rolled
int sixes = 0; // number of times double six is rolled
//ENTER YOUR CODE FOR THE ALGORITHM HERE
while(count < NUMBER)
{
die1Value = generator.nextInt(6) + 1; //returns 1,2,3,4,5,or 6
die2Value = generator.nextInt(6) + 1; //returns 1,2,3,4,5,or 6
if(die1Value == die2Value)
{
if (die1Value == 1)
snakeEyes++;
else if (die1Value == 2)
twos++;
// Task #1 step 3: To do - code for die1Value = 3, 4, 5, and 6
else if (die1Value == 3)
threes++;
else if (die1Value == 4)
fours++;
else if (die1Value == 5)
fives++;
else if (die1Value == 6)
sixes++;
}
count++;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
