Help us understand the problem. What is going on with this article?

サイコロの目の出る確率が大体1/6になることを実感できるプログラムのメモ

More than 1 year has passed since last update.

この度新たにjavaの勉強を始めて、約二ヶ月が経過。基礎となる変数、配列、メソッドその他のまとめとしてサイコロの目が出る確率の収束を確認できるプログラムを書きました。ターミナル上で実行し、任意の回数を入力すると出目をグラフで表示し、それぞれの確率を算出します。体感的には2万回くらい振ると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("サイコロを振りたい回数を入力してください > ");
        int times = scan.nextInt();

        int[] result = generateDice(times);

        System.out.println("結果を表示します");
        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("グラフで表現すると以下のようになります。");
        drawGraph(result);

        System.out.println("それぞれの目の出る確率は、");
        double[] percentage = calcProbability(result);
        for (int i=0; i<percentage.length; i++){
            System.out.println((i+1) + "の目 : " + 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){//グラフ表示用メソッド
        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;
            }
        }

        for (int i=0; i<=5; i++){
            System.out.print((i+1) + " : ");//出目の表示
            for(int j=0; j<dottimes[i]; j++){//1~dice[i]回*を表示
                System.out.print("*");
            }
            System.out.println("");//改行
        }
    }

    public static double[] calcProbability(int[] arr){//確率計算用メソッド。目のデータを受け取って、それぞれの目の出た確率を返す

        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;
    }


}
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away