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

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...
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...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
User the Scanner class for your input Write a java program to calculate the area of...
User the Scanner class for your input Write a java program to calculate the area of a rectangle. Rectangle Area is calculated by multiplying the length by the width   display the output as follow: Length =   Width = Area = Load: 1. Design (Pseudocode ) 2. Source file (Java file, make sure to include comments) 3. Output file (word or pdf or jpig file)
1) Write a functional program in Java that can calculate the volume and surface area of...
1) Write a functional program in Java that can calculate the volume and surface area of a sphere and a cube 2) Write a procedural program in Java that can calculate the volume and surface area of a sphere and a cube 3) Write an Object Oriented Program in Java that can find the volume and surface area of a sphere and cube
Write a program in java that uses methods to input data for calculation, calculate, and display...
Write a program in java that uses methods to input data for calculation, calculate, and display the results of the calculations. That is, there are at least three methods. The problem is to write a program that calculates the area of a rectangle. This action should be repeatable.
In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total...
In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total number of numbers in the file, Calculate the sum of all the numbers in the file, Calculate the average of all the numbers in the file, Find the smallest value of all the numbers in the file, Find the largest value of all the numbers in the file, Calculate the standard deviation of all the numbers in the file
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT