diff --git a/src/main/java/com/never/basic/zeroOneGenerate.java b/src/main/java/com/never/basic/zeroOneGenerate.java new file mode 100644 index 0000000..f687c8c --- /dev/null +++ b/src/main/java/com/never/basic/zeroOneGenerate.java @@ -0,0 +1,76 @@ +package com.never.basic; + +/** + * @Description: TODO + * @author: Bo Li + * @date: 2022年08月29日 9:13 + */ +public class zeroOneGenerate { + + public static void main(String[] args) { + int[] arr = {0,0}; + for(int i = 0; i < 100000; i++){ + arr[g()] +=1; + } + + for(int i = 0; i < arr.length; i++){ + System.out.println(arr[i]); + } + + } + + /** + * @Description: 实现1-7等概率生成器 + */ + public static int f3(){ + int res = 0 ; + do{ + res = (g()<<2) + (g()<<1)+(g()<<0); + }while(res >6); + return res+1; + } + /** + * @Description: 实现0-7等概率生成器 + */ + public static int f2(){ + return (g()<<2) + (g()<<1)+(g()<<0); + } + /** + * @Description: 实现0、1生成器 + */ + public static int g(){ + int res =0; + do{ + res = f1(); + }while(res == 3); + + return res == 2 || res ==1? 0:1; + } + + /** + * @Description: 模拟外界不可见的1-5等概率随机函数 + */ + public static int f1(){ + return (int)(Math.random()*5)+1; + } + + + /** + * @Description: 通过fr1(),实现01等概率 + */ + public static int g2(){ + int ans = 0; + do{ + ans = fr1(); + }while (ans == fr1()); + return ans; + } + /** + * @Description: 模拟外界不可见的0-1不等概率随机函数 + */ + public static int fr1(){ + return Math.random()<0.7?0:1; + } + + +}