Question

In: Computer Science

Write a java program that simulates thousands of games and then calculate the probabilities from the...

Write a java program that simulates thousands of games and then calculate the probabilities from the simulation results. Specifically in the game, throw two dice, the possible summations of the results are: 2, 3, ..., 12. You need to use arrays to count the occurrence and store the probabilities of all possible summations. Try to simulate rolling two dice 100, 1000, 10,0000 times, or more if needed. Choose one simulation number so that the probabilities you calculated is within 1% absolute error compared with the theoretical probability. For example, the theoretical probability for summation result 2 is approximately 2.78%, then your calculated probability based on simulations should be between 2.28% and 3.28%. The following is required:

• Use an array of ints of length 11 to keep count of the difference occurrences. When an int or double array is created, its values are initialized to 0 by default.

• In the simulation loop, throw the two dice, add the values, and then increase the corresponding array element by 1.

• Turn the occurrence counts into probabilities after the simulation loop is done.

Solutions

Expert Solution

import java.util.Random;

public class Simulation {
    
    public static void main(String[] args){

        // Number of times simulation is run.
        int N = 10000000;
        double freq[] = new double[11];

        Random rand = new Random();

        for(int i=0;i<N;i++){
            // Get 2 random numbers in range[1,6].
            int roll1 = rand.nextInt(6) + 1;
            int roll2 = rand.nextInt(6) + 1;

            // sum them and add 1 to corresponding index in freq array.
            int sum = roll1 + roll2;

            freq[sum - 2]++;
        }

        // Find probability for each sum.
        for(int i=0;i<11;i++){
            double probabilty = freq[i]/N;

            System.out.printf("Probabilty of sum %d = %f\n", i+2, probabilty);
        }
    }
}

I have used N = 10^7, and have gotten accurate results.

The output-

Theoretical Probability -

As we can see, the probabilities calculated are very accurate and have error of less than 1 percent.

The code has been commented so you can understand the code better.

I would love to resolve any queries in the comments. Please consider dropping an upvote to help out a struggling college kid :)

Happy Coding !!


Related Solutions

Write a Java program 1-)write a program that simulates a vending machine, takes input from the...
Write a Java program 1-)write a program that simulates a vending machine, takes input from the user (money), and returns the change to the user. The change means Quarter =   25 cent Dime = 10 cent Nickels = 5 cent Pinneies = 1 cent 2-) What is the output produced by the following code? int result = 11; result /= 2; System.out.println("resu lt is " + result);
Using a (GUI interface), write a Java program that simulates an ATM machine with the following...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following options menu: "Welcome" 1. Deposit to account 2. Withdraw 3. Exit
Write a Java program called RevenueAdvanced to calculate the revenue from a sale based on the...
Write a Java program called RevenueAdvanced to calculate the revenue from a sale based on the unit price and quantity of a product input by the user. (use if and if-else statements) • The discount rate is o 0% for the quantity purchased between 1 and 49 units. o 10% for the quantity purchased between 50 and 99 units. o 15% for the quantity purchased between 100 and 149 units. o 25% for the quantity purchased greater than or equal150...
Java - Create a program that simulates a slot machine. When the program runs, it should...
Java - Create a program that simulates a slot machine. When the program runs, it should do the following: - Ask the user to enter the amount of money he or she wants to enter into the slot machine. - Instead of displaying images, have the program randomly select a word from the following list: Cherries, Oranges, Plums, Bells, Melons, Bars (To select a word, the program can generate a random number in the range of 0 through 5. If...
Java programming language Write a Java application that simulates a test. The test contains at least...
Java programming language Write a Java application that simulates a test. The test contains at least five questions. Each question should be a multiple-choice question with 4 options. Design a QuestionBank class. Use programmer-defined methods to implement your solution. For example: - create a method to simulate the questions – simulateQuestion - create a method to check the answer – checkAnswer - create a method to display a random message for the user – generateMessage - create a method to...
Roll dice java program The program simulates a dice roll by generating a random integer between...
Roll dice java program The program simulates a dice roll by generating a random integer between 1 and 6
In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester.    The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT