【发布时间】:2015-04-09 15:02:44
【问题描述】:
在下面的代码中,我预计一个骰子会扮演数十亿次的角色,平均结果正好是 3.5,高于 3.5 的百分比有时是 5%,而其他时候(当然有不同的种子)是就像 95。但即使你达到 6040M 的高度,你也永远不会超过 50%,低于 3.5 的 50%?显然 rand() 有一点偏差...
我知道“真正的随机”并不存在,但它真的这么明显吗?
典型的输出是:
平均:3.50003 计数器:3427000000 百分比高于:83.2554 Perc abs 高于计数器:50.0011
平均:3.49999 计数器:1093000000 高于百分比:92.6983 Perc abs 高于计数器:50.0003
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <unistd.h>
#include <iostream>
using namespace std;
int main ()
{
long long int this_nr;
long long int counter = 0;
long long int above_counter = 0;
long long int below_counter = 0;
long long int above_counter_this = 0;
long long int below_counter_this = 0;
long long int interval_counter = 0;
double avg = 0.0;
srand (time(NULL));
srand (time(NULL));
srand (time(NULL));
cout.precision(6);
while(1) {
this_nr = rand() % 6 + 1; // 0,1,2,3,4,5 or 6
avg = ((double) this_nr + ((double)counter * (double) avg))
/ ((double) counter+1.0);
if (this_nr <= 3) below_counter_this++;
if (this_nr >= 4) above_counter_this++;
if (avg < 3.5) below_counter++;
if (avg > 3.5) above_counter++;
if (interval_counter >= 1000000) {
cout << "Average: " << avg << " counter: " << counter << " Percentage above: "
<< (double) above_counter / (double) counter * 100.0
<< " Perc abs above counter: " << 100.0 * above_counter_this / counter
<< " \r";
interval_counter = 0;
}
//usleep(1);
counter++;
interval_counter++;
}
}
【问题讨论】:
-
请注意所有投掷的平均值与高于 3.5 的投掷百分比之间的差异。我担心平均值,永远不会达到 50% 左右
-
this_nr = rand() % 6 + 1; // 0,1,2,3,4,5 or 6没有正确描述输出。使用代码中的+ 1,输出0是不可能的。您可能的输出是:仅 1、2、3、4、5、6。 -
你说的很对。不过,评论是错误的部分。我的期望确实是 1、2、3、4、5 或 6。谢谢!
-
顺便说一句,您确实意识到多个 srand() 毫无意义?至少它们在循环之外是正确的——我们必须每周至少修复一次该错误。此外,当您将 interval_counter 归零时,您似乎并未将所有统计信息归零。例如, above_counter 和 below_counter 似乎在不断累积,因此您的试验不是独立的。
-
独立试验...我怎么会忘记:)