rahilm

December 27th, 2009, 08:48 PM

Hi,

I have been reading Frederick Mosteller's Fifty Challenging Problems in Probabilty and i came across this problem.

On the average, how many times a die must be thrown until one gets a 6?sure, the answer is 6. But i wanted to verify (my knowledge of C). I wrote up this program:

/*Average number of throws for a dice to show 6*/

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define NO 1000000

int throw()

{

int i,j=0;

for(i=1;j!=6;i++)

j=rand()%6+1;

return i;

}

int main()

{

int i,sum=0;

srand((unsigned)time(NULL));

for(i=0;i<NO;i++)

sum+=throw();

printf("%f",(float)sum/NO);

return 0;

}

What i find uncomfortable, is that almost everytime, the answer is close to 7 and not 6.

i tried it 5 times to get:

7.006066

6.997382

7.003007

6.996360

7.012870

So my question is , is it correct? is the random number generator supposed to give results according to probability theory (in this case it doesn't)? I tried many more probablity problems, they all agreed to theory. I hate probability, and this is the only way to make it interesting for me.

I have been reading Frederick Mosteller's Fifty Challenging Problems in Probabilty and i came across this problem.

On the average, how many times a die must be thrown until one gets a 6?sure, the answer is 6. But i wanted to verify (my knowledge of C). I wrote up this program:

/*Average number of throws for a dice to show 6*/

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define NO 1000000

int throw()

{

int i,j=0;

for(i=1;j!=6;i++)

j=rand()%6+1;

return i;

}

int main()

{

int i,sum=0;

srand((unsigned)time(NULL));

for(i=0;i<NO;i++)

sum+=throw();

printf("%f",(float)sum/NO);

return 0;

}

What i find uncomfortable, is that almost everytime, the answer is close to 7 and not 6.

i tried it 5 times to get:

7.006066

6.997382

7.003007

6.996360

7.012870

So my question is , is it correct? is the random number generator supposed to give results according to probability theory (in this case it doesn't)? I tried many more probablity problems, they all agreed to theory. I hate probability, and this is the only way to make it interesting for me.