473,414 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Curious probabilities

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 <math.h>
#include <stdio.h>
#include <stdlib.h>

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
3 1573
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. It
is 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 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.
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 <math.h>
#include <stdio.h>
#include <stdlib.h>

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);
}

int
main(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++ compiler
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);


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
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 <math.h>
#include <stdio.h>
#include <stdlib.h>

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
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?

<snip>

--
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Randell D. | last post by:
Folks, A ng poster recently questioned their usage/creation of arrays and their correct syntax. I got the idea to performance test from a recent (excellent) PHP Tutorial article that was in Linux...
0
by: steve | last post by:
Hi, I am running phpmyadmin v2.5.6, and if I try to edit records, and some of those records have long text data (e.g. 1000 characters) in text fields, then edit shows a blank page. I managed to...
0
by: James Andrus | last post by:
I don't know anything about making web pages, .NET, etc. I have noticed that my Norton Internet Security (2003) ad/popup blocker is blocking the loading of .aspx pages. The results of a Google...
1
by: nick | last post by:
Hi all, I've a curious problem at the moment and was wondering whether anyone might be able to help. I have a SOAPBodyElement object that was returned from a SOAP call. I then convert this into...
0
by: theintrepidfox | last post by:
Hi Group It's a rainy day and because of pure boredom have right-clicked a row in EM and selected 'Properties'. Just being curious what the dialog coming up is all about (call me ignorant!)...
5
by: Stu | last post by:
When I have a form in view mode, the buttons at the bottom open several other forms, such as a contact list, and a "home" page. If I leave the cursor over the button for a second, the little...
40
by: Confused User | last post by:
I am curious what the origins of size_t are. I see that it is usually typedef'd to be the native integer size of a particular machine. I also see that routines like strncpy(char *t, const char *s,...
7
by: tshad | last post by:
I was curious if there is some reason why you don't need the "then" in the if test of VB.Net or is it just ASP.NET. I just noticed that I don't really need to explicitly put the word "then" as...
11
by: Liz | last post by:
anyone have any idea why there have been (at least) 82 VS 2008 post-release messages here on the C# board but only 5 on the VB.NET board; are the VB crowd just not interested?? strikes me as a...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.