By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 424,982 Members | 1,955 Online
+ Ask a Question
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]) : 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
Share this Question
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]) : 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

 P: n/a Co********@gmail.com wrote: 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, That's incorrect. 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. Probability is successfull outcomes -------------------- total outcomes The "total outcomes", which you are not tracking, increments every time you decide to play. And seperately, the wins and losses should be tallied. You CANNOT have 1 win and 0 outcomes which is where the confusion is. And it is wrong to figure probability by dividing the wins by the losses, regardless of whether or not you get a divide by 0 error. In this sample, where I chose to play regardless of whether I won or not, the correct probability would be 6/10, not 6/4 (which you see as 1 instead of 1.5 because you printed the ratio as an integer). Guess my number, its between 0 and 2: 1 My number was 2, do you still want to play (1/0)? 1 You want to play? Your total losses are 1 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 1 You won, your total wins are: 1 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 1 You won, your total wins are: 2 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 1 You won, your total wins are: 3 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 1 You won, your total wins are: 4 Guess my number, its between 0 and 2: 1 My number was 0, do you still want to play (1/0)? 1 You want to play? Your total losses are 2 Guess my number, its between 0 and 2: 1 My number was 0, do you still want to play (1/0)? 1 You want to play? Your total losses are 3 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 1 You won, your total wins are: 5 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 1 You won, your total wins are: 6 Guess my number, its between 0 and 2: 1 My number was 2, do you still want to play (1/0)? 1 You want to play? Your total losses are 4 Wins: 6 Didn't win: 4 Wins / Didn't Win: 1 In this run, I chose to play only when I won. Yes, there is a 0 in the "didn't win" variable, but you aren't supposed to divide by that. The total outcomes should be the count of the times I played, which was 6. The correct probability would then be 6/6 which doesn't give you a divide by 0 error. The number of times I lost was 0 (because I chose not to play) and it's probability is 0/6 which still does not give you a divide by 0 error. Guess my number, its between 0 and 2: 1 My number was 2, do you still want to play (1/0)? 0 Good decision, your total losses are 0 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 1 You won, your total wins are: 1 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 1 You won, your total wins are: 2 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 1 You won, your total wins are: 3 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 1 You won, your total wins are: 4 Guess my number, its between 0 and 2: 1 My number was 0, do you still want to play (1/0)? 0 Good decision, your total losses are 0 Guess my number, its between 0 and 2: 1 My number was 0, do you still want to play (1/0)? 0 Good decision, your total losses are 0 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 1 You won, your total wins are: 5 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 1 You won, your total wins are: 6 Guess my number, its between 0 and 2: 1 My number was 2, do you still want to play (1/0)? 0 Good decision, your total losses are 0 Wins: 6 Didn't win: 0 Floating point exception (core dumped) Finally, in this example, I chose to play only 5 times. I won 4 times, lost once. The correct probalilities are win 4/5 lose 1/5 played 5/10 Guess my number, its between 0 and 2: 1 My number was 2, do you still want to play (1/0)? 1 You want to play? Your total losses are 1 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 1 You won, your total wins are: 1 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 1 You won, your total wins are: 2 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 1 You won, your total wins are: 3 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 1 You won, your total wins are: 4 Guess my number, its between 0 and 2: 1 My number was 0, do you still want to play (1/0)? 0 Good decision, your total losses are 1 Guess my number, its between 0 and 2: 1 My number was 0, do you still want to play (1/0)? 0 Good decision, your total losses are 1 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 0 You don't want to play? Your total losses are 1 Guess my number, its between 0 and 2: 1 My number was 1, do you still want to play (1/0)? 0 You don't want to play? Your total losses are 1 Guess my number, its between 0 and 2: 1 My number was 2, do you still want to play (1/0)? 0 Good decision, your total losses are 1 Wins: 4 Didn't win: 1 Wins / Didn't Win: 4 #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]) : 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 11 '06 #3

 P: n/a Co********@gmail.com wrote: 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. this sounds perilously close to a causality violation. Was your computer travelling faster than light when it performed this computation? -- Nick Keighley As I recall, OSI dealt with TCP/IP by just admitting it into the spec as a variation of existing levels. This is akin to dealing with an Alien face hugger by allowing it to implant its embryo in your body. Apr 11 '06 #4

### This discussion thread is closed

Replies have been disabled for this discussion.