Solving Probability Project: Acceptable Error Rate?

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
6 replies · 3K views
magnifik
Messages
350
Reaction score
0
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:
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;
}
 
Physics news on Phys.org
You can introduce the swapping error into your code like so:

Code:
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?
 
just wondering.. why do you use (rand()%100) + 1?
 
Last edited:
magnifik said:
just wondering.. why do you use (rand()%100) + 1?

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.
 
so regardless of the percent error, you would still use rand()%100 + 1?
 
magnifik said:
so regardless of the percent error, you would still use rand()%100 + 1?

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.
 
is it unnecessary information to have the 520000 people who voted for A and the 480000 people who voted for B?