Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Probability program

  1. Jul 11, 2010 #1
    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. jcsd
  3. Jul 11, 2010 #2
    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?
     
  4. Jul 12, 2010 #3
    just wondering.. why do you use (rand()%100) + 1?
     
    Last edited: Jul 12, 2010
  5. Jul 12, 2010 #4
    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.
     
  6. Jul 12, 2010 #5
    so regardless of the percent error, you would still use rand()%100 + 1?
     
  7. Jul 12, 2010 #6
    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.
     
  8. Jul 13, 2010 #7
    is it unnecessary information to have the 520000 people who voted for A and the 480000 people who voted for B?
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook