package com.ketty;
public class RandomTORandom {
// 随机等概率返回1~7之间的整数
public static int f1(){
return (int)(Math.random()*7)+1;
}
//等概率实现0~1的对数器
public static int f2(){
int ans=0;
do {
ans=f1();
}while (ans==4);
return ans< 4?0:1;
}
//做到000~1111为等概率 0~15
public static int f3(){
return (f2()<<0)+(f2()<<1)+(f2()<<2)+(f2()<<3);
}
//做到 0~9 之间的等概率 大于9,重新跑
public static int f4(){
int ans=0;
do {
ans=f3();
}while (ans>9);
return ans;
}
//做到 1~10 之间的等概率
public static int g(){
return f4()+1;
}
public static void printNum(){
int[] count=new int[11];
for (int i = 0; i < 10000000; i++) {
int num=g();
count[num]++;
}
for (int i = 0; i < 11; i++) {
System.out.println(i+"这个数,出现了"+count[i]+"次");
}
}
//验证 0~1 对数器的概率
public static void fx(){
int count1=0,count2=0;
for (int i = 0; i < 1000000; i++) {
if (f2()==0){
count1++;
}else{
count2++;
}
}
System.out.println((double) count1/(double)1000000);
System.out.println((double)count2/(double)1000000);
}
//打印1~10随机整数的概率
public static void main(String[] args) {
printNum();
}
}