在下面的代码中,我期望一个角色数量达到平均结果正好为3.5的骰子,有时高于3.5的百分比是5%,其他时间(当然是不同的种子)是喜欢95.但即使你高达6040M左右,你也永远不会高于50%,低于3.5%?显然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++;
}
}