Thread: Probabilty Question

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    Probabilty Question

    I need to Generate 1000 random numbers between 0 and 1. I got this part.
    Next i need to identify the probabilty of having a head or a tail if i flipped a coin.
    Also i dentify the probabibilty of a side of a six faced die.


    I need [to use random numbers\
    Code:
    #include <stdio.h>
    #include<stdlib.h>
    
    int main()
    {
    float sum=0, average;
    int i;
    for (i=0; i<1000; i++) sum=sum;
    
    for (i=0 ;i<1000; i++)
    printf("%f\n", 1.0*rand()/RAND_MAX);
    printf("\nMAX = %d\n" , RAND_MAX);
    
    
    printf("The probability for having a Heads or Tails is:\n");
    printf ("HEADS               TAILS\n");
    printf ("----                -----\n");
    printf("   %d                  %d\n");
    
    
    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The probability is 1 in 2, and 1 in 6. I'm not sure what you really need to compute here.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    as quzah said, the probability is 1/2 and 1/6, for your two problems respectively. therefore you dont have to calculate "the probability".

    what you probably need to do is an experiment, and determine "the probability of [...] in n = 1000 trials". so, for the coin "experiment", you flip the coin (generate a (pseudo)random number with 2 possible values) and keep track of how many times it turns out heads ("value 1") and how many times it turns out tails ("value 2"). then simply calculate the percentage of each, from the total of 1000 tries. do a similar thing for the next "experiment".

    in the case of only two values, you only actually have to keep track of 1 of the values. for example, if, out of 1000 tests, there were 100 tails, you obviously know how many heads there must have been (EDIT: recall that since 1 = P + Q, Q = 1 - P). similarly, for the other experiment, you only need to track 5 of the 6 totals, but im sure just keeping track of all of them isnt a big deal.

    after you are finished, you might find it interesting that when you increase the number of tests run for either experiment, they become closer and closer to the "actual" probabilities. try n = 1, 1000, 1000000, etc.
    Last edited by nadroj; 10-20-2009 at 05:12 PM.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Quote Originally Posted by nadroj View Post
    as quzah said, the probability is 1/2 and 1/6, for your two problems respectively. therefore you dont have to calculate "the probability".

    what you probably need to do is an experiment, and determine "the probability of [...] in n = 1000 trials". so, for the coin "experiment", you flip the coin (generate a (pseudo)random number with 2 possible values) and keep track of how many times it turns out heads ("value 1") and how many times it turns out tails ("value 2"). then simply calculate the percentage of each value, out of the total of 1000 tries. do a similar thing for the next "experiment".

    in the case of only two values, you only actually have to keep track of 1 of the values. for example, if, out of 1000 tests, there were 100 tails, you obviously know how many heads there must have been. similarly, for the other experiment, you only need to track 5 of the 6 totals, but im sure just keeping track of all of them isnt a big deal.

    after you are finished, you might find it interesting that when you increase the number of tests run for either experiment, they become closer and closer to the "actual" probabilities. try n = 1, 1000, 1000000, etc.

    Yeah i see what you mean . I know i have to write a program that tells me out of 100 flips how many heads and tails i get.Any Suggestions?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    you mean 1000. and i already gave you my (i thought) detailed suggestion. expand on your code further or write some pseudocode to solve the problem, and we can help you translate it to code. otherwise, just post your code after youve expanded it and let us know the specific problem.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if heads
        heads++
    else
        tails++
    The same sort of thing (though I'd just use an array) for your dice sides.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    I figured out the first part

    I figured out the first part of the question any help with the second part?




    Code:
     
    #include <stdio.h>
    #include <time.h>
    
    int flip( );
    int main( void )
    {
    
    int i;
    int heads = 0;
    int tails = 0;
    
    srand(time(NULL));
    
    for ( i= 1; i<= 1000; i++ ){
        printf( "%d\n", flip( ));
    
        if (flip( ) == 0)
        heads++;
        else
        tails++;
    
    }
    printf("heads     tails \n");
    printf("-----------------\n");
    printf("%d          %d \n",heads,tails);
    return 0;
    
    }
    int flip( )
    {
        int i = rand() % 2;
            if (i == 0)
                 return 0;
            else
                 return 1;
           }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Is it the probability math that you're stuck on?

    that's just the

    number of times the coin was heads / number of times the coin was flipped.

    So if you fliped it 100 times, and got heads 52, your probability would be:

    52/100 = .52

    And since percent means hundredths, that's 52%

    For rolling dice, it's the same thing.

    Number of rolled 3's / number of times the die was rolled, or

    17/100 = .17, or 17%

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM