JAva確率Demo

1421 ワード

確率は55%、20%、20%、5%です {0,1,2,3}
 
package ver;

import java.util.Random;

public class CZ {
	//           
	int count_0, count_1, count_2, count_3; // 4     

	Random r = new Random();	//      

	//    :           ,          
	public void calc(int count) {
		int num;
		for(int i=0; i<count; i++) {
			num = r.nextInt(100) + 1;	//     1~100      
			if(num <= 55) {		//55%
				count_0++;
			} else if(num <= 75) {		//20%
				count_1++;
			} else if(num <= 95) {		//20%
				count_2++;
			} else if(num <= 100) {		//5%
				count_3++;
			}
		}
		System.out.println("   " + count + "      :");
		System.out.println("0     : " + ((float)count_0/count*100) + "%");
		System.out.println("1     : " + ((float)count_1/count*100) + "%");
		System.out.println("2     : " + ((float)count_2/count*100) + "%");
		System.out.println("3     : " + ((float)count_3/count*100) + "%");

		//     
		count_0 = count_1 = count_2 = count_3 = 0;
	}

	public static void main(String[] args) {
		CZ t = new CZ();
		t.calc(100);
		t.calc(200);
		t.calc(500);
		t.calc(1000000000);
	}

}