424,982 Members | 1,955 Online Need help? Post your question and get tips & solutions from a community of 424,982 IT Pros & Developers. It's quick & easy.

Curious probabilities

 P: n/a I wrote a program that shows some incredibly curious probabilities. It is a simple game where you guess a number between 0 and 2 that the computer thinks up. What is origonal about this game is the computer tells you before hand if you got the number right and you can decide if you want to play or not. The computer keeps track of all your wins, all your losses, and as is possible all of your didn't wins but didn't loses. What is strange about this program is that the total probability of you winning the game can be undefined if you never lose, but play a number of undefined games where you haven't won or lost. Please tell me what is going on here, because it is very curious. #include #include #include int gen_random(long int M) { double r; double x; int y; int count; r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) ); x = (r * M); y = (int) x; return(y); } int main(int argc, char *argv[]) { int N = ((argc == 2) ? atoi(argv) : 1); int result = 0; srand(N); /*initialize random number generator*/ int win=0; int notwin=0; for(int cnt=0; cnt<10; cnt++){ int play=0; int R=0; R=gen_random(3); int guess=0; printf("Guess my number, its between 0 and 2: "); scanf("%d",&guess); printf("My number was %d, do you still want to play (1/0)? ", R); scanf("%d", &play); if(R==guess && play==1){ win++; printf("You won, your total wins are: %d\n",win); } if(R==guess && play==0){ printf("You don't want to play? Your total losses are %d\n",notwin); notwin=notwin; } if(R!=guess && play==0){ notwin=notwin; printf("Good decision, your total losses are %d\n",notwin); } if(R!=guess && play==1){ notwin++; printf("You want to play? Your total losses are %d\n",notwin); } } printf("Wins: %d\n",win); printf("Didn't win: %d\n",notwin); printf("Wins / Didn't Win: %d\n", win/notwin); return(0); } Apr 10 '06 #1
3 Replies

 P: n/a On 10 Apr 2006 15:06:57 -0700, in comp.lang.c , Co********@gmail.com wrote: I wrote a program that shows some incredibly curious probabilities. Itis a simple game where you guess a number between 0 and 2 Lets assume you mean "inclusive", since there's only one whole number between nought and two..... :-) What is strange about this program is that the totalprobability of you winning the game can be undefined if you never lose,but play a number of undefined games where you haven't won or lost. No, the probability is always the same - assuming you have a decent RNG. The percentage of times you have already won is a random variable however. Is that what you mean? Please tell me what is going on here, because it is very curious. I have a feeling you have a misunderstanding of the statistics and/or algorithm. #include #include #include int gen_random(long int M) { double r; double x; int y; int count; not used r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) ); x = (r * M); y = (int) x; return(y);}intmain(int argc, char *argv[]) { int N = ((argc == 2) ? atoi(argv) : 1); int result = 0; srand(N); /*initialize random number generator*/ you can't have executable statements before variable declaraations in in C89. You are probably using a C++ compilerint win=0;int notwin=0;for(int cnt=0; cnt<10; cnt++){int play=0;int R=0;R=gen_random(3);int guess=0;printf("Guess my number, its between 0 and 2: ");scanf("%d",&guess); don't use scanf to read user input. Its a bad choice. Read the FAQ for some suggestions. Mark McIntyre -- "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian Kernighan Apr 10 '06 #2 