我应该使用什么方法来最小化抛硬币的数量,以生成从0到9的均匀分布数字?
我做了一些研究,但是我还没有能够解决如何适应这个具体问题的方法:
我做了一些研究,但是我还没有能够解决如何适应这个具体问题的方法:
randNum
返回0-9之间均匀分布的随机数。rand2
通过将T和H分配为0和1或反之来模拟抛硬币的过程。int[][][][] fourDimArr = { { { {1, 2},{3, 4} }, {{5, 6} ,{7, 8} } }, { { {9,10},{0,0} }, { {0,0},{0,0} } } };
int result = 0;
while (result == 0)
{
int i = rand2();
int j = rand2();
int k = rand2();
int l = rand2();
result = fourDimArr[i][j][k][l];
}
int randNum = result-1;
int result = 11;
while(result>=10){
result = 0;
for(int j = 0; j < 4; j++){
result = (result<<1)|rand2();
}
}
randNum = result;
private static int rand2() {
if(Math.random()>0.5)return 1;
return 0;
}