1. 程式人生 > >擲6面骰子6000次,每個點出現的概率

擲6面骰子6000次,每個點出現的概率

import java.util.Random;

public class Statistics {
	
	final static int  Maxsize = 6000;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Random rand = new Random();
		
		int temp[] = new int[Maxsize];
		
		for(int i = 0; i < Maxsize; i++)
			temp[i] = rand.nextInt(6) + 1;
		
		int a=0, b=0, c=0, d=0, e=0, f=0;
		
		for(int i = 0; i < temp.length; i++)
		{
			if(temp[i] == 1)
				a++;
			else if(temp[i] == 2)
				b++;
			else if(temp[i] == 3)
				c++;
			else if(temp[i] == 4)
				d++;
			else if(temp[i] == 5)
				e++;
			else if(temp[i] == 6)
				f++;
		}
		
		System.out.println("1出現:" + a + " 2出現:" + b + " 3出現:" + c + " 4出現:" + d + " 5出現:" + e + " 6出現:" + f + "\n");
		
		float one = (float)a/Maxsize, two = (float)b/Maxsize, three = (float)c/Maxsize, four = (float)d/Maxsize;
		float five = (float)e/Maxsize, six = (float)f/Maxsize;
		
		System.out.println(1 + "出現的概率是:" + one);
		System.out.println(2 + "出現的概率是:" + two);
		System.out.println(3 + "出現的概率是:" + three);
		System.out.println(4 + "出現的概率是:" + four);
		System.out.println(5 + "出現的概率是:" + five);
		System.out.println(6 + "出現的概率是:" + six);
	}

}
結果: