增加Math.random拓展案例,0、1生成器、随机概率与等概率的转换

master
libospinach 2 years ago
parent 38a51bdcfa
commit 8609a42a11

@ -0,0 +1,76 @@
package com.never.basic;
/**
* @Description: TODO
* @author: Bo Li
* @date: 20220829 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: 01
*/
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;
}
}
Loading…
Cancel
Save