1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

この度新たに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;
    }
    
    
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?