好的,很难在标题中解释我的问题。我希望这足以激发你们中的一些人的好奇心。
我一直在尝试做的是找到生成的随机数 (n) 的数量与生成相等数量的奇数和偶数的概率之间的关系。(n/2 奇数和 n/2 偶数)。应该清楚的是,n 必须是偶数。
显然,数字不必是奇数和偶数,只要有 50% 的机会生成其中一个即可。如果这张贴在错误的位置,有人可以引导我朝正确的方向前进。
无论如何,基本上我从数学上计算出,对于生成的每 n 个数字,在结果达到完美的 50/50(奇数和偶数)之前,平均应该进行大约 (4/5)sqrt(n) 次试验。
我使用斯特林的阶乘近似得到了 sqrt(n) 的结果,因为我认为最终结果会更容易理解并且更容易与我的计算结果进行比较。
但是,我计算的平均值给了我大约 (6/5)sqrt(n) 的结果,我不知道为什么。我使用了两个不同的伪随机生成器(Math.random() 和 java.util.Random())并且都产生了相同的结果。
下面是计算概率所涉及的数学,我也会提供代码。
如果看不懂请放大图片。

用于计算计算的计算机程序:
public static void main(String[] args) {
int run = 0;
int total = 0;
int amount = 0;
int count = 0;
int randint;
int odd;
int even;
int n =100; // Amount of numbers generated randomly
int precision = 10000; // Amount of averages taken into account
// (Reduce with increase in "n" as time increases)
Random generate = new Random(100); // Seed 100 at beginning of program
run = 1;
while(run==1){
odd = 0;
even = 0;
for(int i=0; i<n; i++){
randint = generate.nextInt(2);
if(randint%2==0){
even++;
}else{
odd++;
}
}
count++; // increment count after every check
if((odd==(n/2)) && (even==(n/2))){
total+=count; // add count to total ( addition before addition - average calculation)
amount++; // increment amount, this will be the division later
count=0;
}
if(amount==precision){
System.out.println(total/amount); //print average
run=0;
}
}
}