473,785 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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",&gue ss);
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 1598
On 10 Apr 2006 15:06:57 -0700, in comp.lang.c , Co********@gmai l.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 misunderstandin g 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("Gues s my number, its between 0 and 2: ");
scanf("%d",&gu ess);


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********@gmai l.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",&gue ss);
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********@gmai l.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
2591
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 Format magazine (which dealt with performance). The original poster had a reply from someone who had said using $testArray was better (and proper) than $testArray I *had* believed this to be correct... but... against my better judgement, I
0
1439
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 get it working, by doing a sql query which selects a single field, and then "edit"ing the result set. Amazing, but it manages to work. Anyone seen this?
0
277
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 search suggest that .aspx pages are a feature of MS .NET (please don't flame me if I am wrong). Is this to be expected with .aspx pages? Is there a simple explanation for this? I am curious. Thanks.
1
1174
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 a Document object and then attempt to apply a XSLT file to it. Unfortunately the output produced by this transformation is incorrect. Effectively the XSLT is not being applied correctly. Bizarrely if I serialize the document to a byte array and...
0
932
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!) and where I can find more information (Keyword?)in BOL? One thing I wonder however and maybe someone can answer this question is that this dialog suggests that a table is nothing more than a query
5
1135
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 yellow box comes up but tells me "open form" since I used the wizard to create code to open the form. Can this text in the yellow box be changed from "open form" to "contact list" or "home"? If this can be done, how would I go about it? This of...
40
2550
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, size_t n). So, is size_t the native size of the machine or used to describe the actual size of something (like the len of a string copy). If it is named because it designates the size of something, then could I still have more than 65535...
7
1226
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 part of my if statement. For example: if a = b b= 10
11
1361
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 significant difference but I'm not sure what to make of it ....
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10148
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.