In the following code, in which I expect a die that roles bilions of times that the average outcome to be exactly 3.5, the percentage that lies above 3.5 sometimes is like 5 percent and other times (with different seed of course) is like 95. But even when you go as high as 6040M thows, you never end up near 50% above, 50% under 3.5? Obviously there's a little bias in rand()...
I know about the fact that 'real random' doesn't exist but is it really this obvious?
Typical outputs are:
Average: 3.50003 counter: 3427000000 Percentage above: 83.2554 Perc abs above counter: 50.0011
Average: 3.49999 counter: 1093000000 Percentage above: 92.6983 Perc abs above counter: 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++;
}
}