A memo of the program that allows you to realize that the probability of dice rolling is about 1/6

About two months have passed since I started studying java anew. I wrote a program that can confirm the convergence of the probability of rolling dice as a summary of the underlying variables, arrays, methods and so on. Execute it on the terminal, enter any number of times, the result will be displayed as a graph, and the probability of each will be calculated. If you shake it about 20,000 times, it will be very close to 1/6.

Dice_Probability.java


import java.util.Scanner;
import java.util.Random;

public class Dice_Probability{
    public static void main(String[] args){
    	Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter the number of times you want to roll the dice> ");
        int times = scan.nextInt();
        
        int[] result = generateDice(times);
        
        System.out.println("Display the result");
        for (int i=0; i<result.length; i++){
        	System.out.printf("%2d", result[i]);
            
            if((i+1) % 10 ==0){
            	System.out.println("");
            }
        }
        
        System.out.println("The graph is as follows.");
        drawGraph(result);
        
        System.out.println("The probability of each eye appearing is");
        double[] percentage = calcProbability(result);
        for (int i=0; i<percentage.length; i++){
        	System.out.println((i+1) + "Eyes: " + percentage[i]);
        }
    }
    
    public static int[] generateDice(int n){
    	Random rand = new Random();
        int[] arr = new int[n];
        
        for (int i=0; i<arr.length; i++){
        	arr[i] = rand.nextInt(6) + 1;
        }
        
        return arr;
    }
    
    public static void drawGraph(int[] arr){//Graph display method
        int[] dottimes = {0,0,0,0,0,0};//Record the number of times the eyes appear
        for (int i=0; i<arr.length; i++){
            switch (arr[i]){
                case 1:
                    dottimes[0]++;
                    break;
                case 2:
                    dottimes[1]++;
                    break;
                case 3:
                    dottimes[2]++;
                    break;
                case 4:
                    dottimes[3]++;
                    break;
                case 5:
                    dottimes[4]++;
                    break;
                case 6:
                    dottimes[5]++;
                    break;
            }
        }
        
        for (int i=0; i<=5; i++){
            System.out.print((i+1) + " : ");//Display of rolls
        	for(int j=0; j<dottimes[i]; j++){//1~dice[i]Times*Show
            	System.out.print("*");
            }
            System.out.println("");//new line
        }
    }
    
    public static double[] calcProbability(int[] arr){//Method for calculating probability. Receives eye data and returns the probability of each eye
        
        int[] dottimes = {0,0,0,0,0,0};
        for (int i=0; i<arr.length; i++){
            switch (arr[i]){
                case 1:
                    dottimes[0]++;
                    break;
                case 2:
                    dottimes[1]++;
                    break;
                case 3:
                    dottimes[2]++;
                    break;
                case 4:
                    dottimes[3]++;
                    break;
                case 5:
                    dottimes[4]++;
                    break;
                case 6:
                    dottimes[5]++;
                    break;
            }
        }
        
        double[] probability = {0,0,0,0,0,0};
        
        for (int i=0; i<dottimes.length; i++){
            probability[i] = (double)dottimes[i]/arr.length;
        }
        
        return probability;
    }
    
    
}

Recommended Posts

A memo of the program that allows you to realize that the probability of dice rolling is about 1/6
A memo that was soberly addicted to the request of multipart / form-data
A memo when you want to clear the time part of the calendar
To you who lament that the conversion of JODConverter + LibreOffice is slow
A program that determines whether the entered integer is close to an integer
Creating an ArrayList that allows you to throw in and retrieve the coordinates of a two-dimensional plane
About the matter that tends to be confused with ARG of Dockerfile which is a multi-stage build
A simple application (CT) that allows you to view the RDSR in which the radiation exposure dose is recorded.
[Ruby] I want to make a program that displays today's day of the week!
A memo about the types of Java O/R mappers and how to select them
Determine that the value is a multiple of 〇 in Ruby
A program that counts the number of words in a List
A program that searches for a character string, and when the search character string is found, displays the character string from the beginning of the line to just before the search character string.
The story of making a binding for libui, a GUI library for Ruby that is easy to install
About the solution to the problem that the log of logback is not output when the web application is stopped
[Beginner] About "Rectangle selection (block selection)" that allows you to correct indentation of multiple lines at once
How to deal with the type that I thought about writing a Java program for 2 years
A memo that containerizes the simple chat application of Node.js + socket.io
How to identify the path that is easy to make a mistake
[Android] Develop a service that allows university students to check the operating status of buses circulating in the university.
[Java] You might be happy if the return value of a method that returns null is Optional <>
Sample program that returns the hash value of a file in Java
We have released a service that allows you to easily create chats!
What is testing? ・ About the importance of testing
A program that calculates factorials from 2 to 100
I tried to make a program that searches for the target class from the process that is overloaded with Java
What to do about "A server is already running ..." that happened without turning off the rails server in the terminal
What to do when you want to delete a migration file that is "NO FILE"
Until you run a Java program with the AWS SDK local to Windows
Androd: What to do about "The Realm is already in a write transaction in"
About the solution of the error that occurred when trying to create a Japanese file of devise in the Docker development environment
Make a margin to the left of the TextField
Set the time of LocalDateTime to a specific time
A story about making a Builder that inherits the Builder
A memo for myself that object-oriented is something
A description of Interface that is often mistaken
We have released "Code-sell", a service that allows you to sell codes! (Personal development)
[Solution] A memo that I had a hard time because the format of sinatra-validation changed
The end of catastrophic programming # 03 "Comparison of integers, if" a> b ", assume that it is" a --b> 0 ""
[Maven] What to do if you are asked to incorporate a jar that is not in the remote repository into the war