Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: (off topic) Is my math right?

Author: Angrim

Date: 11:49:33 08/15/03

Go up one level in this thread


On August 13, 2003 at 13:44:16, Erik Bergren wrote:

>On August 11, 2003 at 18:44:19, Dieter Buerssner wrote:
>
>>>     Your experimental method for producing
>>>the probability curve was a good idea,
>>>and not at all lazy (as you said).
>>>    However your numbers are incorrect.
>>>I suspect your random number generator
>>>was bad.
>>
>>Yes, that is probable. One would need some rather decent random number
>>generator, to get correct numbers in the tails of the distribution. Besides the
>>exact PRNG used by Angrim, also the function flip() would be interesting.
>>
>>Regards,
>>Dieter
>
>Angrim has not yet told how "flip()" was computed.

I'm back at my computer in Montana, so I can answer this :)
I used the simplest method available, the rand() function from glibc
and checking the low-order bit.

Here is the complete code.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int trial(int x){
        int i;
        int j;
        j=0;
        for(i=0;i<x;i++){
                if(rand()&1) j++;
        }
        return j;
}
int main()
{
        int tot=0;
        int i,level;
        int totals[101];

        memset(totals, 0, sizeof(totals));
        srand(time(0));
        for(tot=0;tot<10000000;tot++){
                i=trial(100);
                totals[i]++;
        }
        for(level=50;level<=100;level++){
                if(totals[level]) printf("out of %d trials, %d scored %d\n",
                        tot, totals[level],level);
        }

        return 0;
}

I just tried a slight modification replacing rand()&1 with
(rand()/13)&1 and got much different results.  This suggests
that the low order bit of rand()'s return value is rather weakly
random.

out of 10000000 trials, 790731 scored 50
out of 10000000 trials, 772120 scored 51
out of 10000000 trials, 726285 scored 52
out of 10000000 trials, 656435 scored 53
out of 10000000 trials, 571418 scored 54
out of 10000000 trials, 479676 scored 55
out of 10000000 trials, 386930 scored 56
out of 10000000 trials, 300912 scored 57
out of 10000000 trials, 224220 scored 58
out of 10000000 trials, 161499 scored 59
out of 10000000 trials, 111417 scored 60
out of 10000000 trials, 73127 scored 61
out of 10000000 trials, 46917 scored 62
out of 10000000 trials, 28327 scored 63
out of 10000000 trials, 16556 scored 64
out of 10000000 trials, 9274 scored 65
out of 10000000 trials, 4873 scored 66
out of 10000000 trials, 2484 scored 67
out of 10000000 trials, 1225 scored 68
out of 10000000 trials, 554 scored 69
out of 10000000 trials, 231 scored 70
out of 10000000 trials, 101 scored 71
out of 10000000 trials, 38 scored 72
out of 10000000 trials, 23 scored 73
out of 10000000 trials, 2 scored 74
out of 10000000 trials, 3 scored 75
out of 10000000 trials, 1 scored 76

just to show the variability of the results, the last part of a second run.
out of 10000000 trials, 242 scored 70
out of 10000000 trials, 87 scored 71
out of 10000000 trials, 34 scored 72
out of 10000000 trials, 18 scored 73
out of 10000000 trials, 6 scored 74
out of 10000000 trials, 1 scored 75
out of 10000000 trials, 1 scored 76
out of 10000000 trials, 1 scored 78

Angrim



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.