# Probability program

1. Jul 11, 2010

### magnifik

can anyone help me understand the following project specs? it is supposed to be relatively simply to code, but i can't even figure out what exactly i'm supposed to do.

there are 1000000 people choosing A or B. the process repeats 1000 times. 520000 chose A while 480000 chose B. but there was a 15% error in which B was mistaken for A or A was mistaken for B. is the 15% rate enough to make the results invalid? find the acceptable error rate.

this is what i have so far. i'm not sure how to take into account the error or number of people who actually choose A or B.
Code (Text):

int main(){
int A = 0;
int B = 0;
int aWins = 0;

srand(time(0));
for (int j = 0; j < 1000; j++){
for (int i = 0; i < 1000000; i++){
if ((rand()%2) == 0)
B++;
else
A++;
}
if (A > B)
aWins++;
}
cout << aWins << endl;
cout << (double)(aWins*100)/1000<< endl;
}

2. Jul 11, 2010

### xcvxcvvc

You can introduce the swapping error into your code like so:

Code (Text):
int main(){
int A = 0;
int B = 0;
int aWins = 0;
[B]int errorPercent = 15; // errorPercent% of the time, the comparison will be incorrect
[/B]
srand(time(0));
for (int j = 0; j < 1000; j++){
for (int i = 0; i < 1000000; i++){
if ((rand()%2) == 0)
[B]if( (rand()%100) + 1 > errorPercent )
B++;
else
A++;[/B]
else
[B]if( (rand()%100) + 1 > errorPercent )
A++;
else
B++;[/B]
}
if (A > B)
aWins++;
}
cout << aWins << endl;
cout << (double)(aWins*100)/1000<< endl;
}
Though, I'm not sure how you could find what is being asked... seems kind of silly. Maybe you just compare different levels of swapping error until you see a large difference between no swapping error and that level of swapping error?

3. Jul 12, 2010

### magnifik

just wondering.. why do you use (rand()%100) + 1?

Last edited: Jul 12, 2010
4. Jul 12, 2010

### xcvxcvvc

rand()%100 + 1 returns a number from 1 to 100.
rand()%100 alone returns a number from 0 to 99.

We can examine a smaller example so we can work through the logic step by step:
rand()%3
A random integer has three possibilities: it is evenly divided by 3 (remainder 0), it has a remainder of 1 after division by 3, or it has a remainder of 2 after division by 3. Therefore, we see that rand()%n returns a number from 0 to n-1. Adding 1 brings this to a more "regular" range of 1 to n.

Then, in my code, I say that if a random number from 1 to 100 is greater than the error percent (15 in this case), then there is no swap. 16-100 are greater than that (which is exactly 85 numbers). 1 to 15 are below or equal to it (15 numbers). So we can see that the comparison yields true 85% of the time and false 15% of the time.

5. Jul 12, 2010

### magnifik

so regardless of the percent error, you would still use rand()%100 + 1?

6. Jul 12, 2010

### xcvxcvvc

Yes. The program will work for any realizable percent error (0-100). I haven't thought about how it behaves past 100% error or for negative percents, because that has no practical use here.

7. Jul 13, 2010

### magnifik

is it unnecessary information to have the 520000 people who voted for A and the 480000 people who voted for B?

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Threads - Probability program Date
Fermi Dirac probability Sep 9, 2016
Conditional probability? Feb 24, 2016
Dynamic Programming - World Series Probability Example Mar 19, 2012