Thread: Getting a percentage from a bunch of random numbers?

  1. #1
    kidkash
    Guest

    Question Getting a percentage from a bunch of random numbers?

    Suppose i generate a whole bunch of random numbers and i want to get the percentage of how many times a single number is generated. Here's what i got so far...
    Code:
    int randomcoinflip(int numberofcointosses)
    {
    int i;
    srand(time(NULL));
    for (i = 0; i < numberofcointosses; i++)
    (rand()%2+1);
    return 0;
    }
    Basically i'm generating a bunch of random coin tosses where 1 would be heads and 2 would be tails. How would i get a percentage of the number of times heads shows up in say 40 tries?

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Not quite sure what you mean, but you could put all results in an array and then use that array to calculate the percentage tail and head.

    percentage_head = (nr_of_head / nr_of_tosses) * 100;
    percentage_tail = (nr_of_tail / nr_of_tosses) * 100;

  3. #3
    kidkash
    Guest
    basically, i need a program that would, for example, count the number of time heads shows up in 40 tries and then do a percentage of times it does show up instead of tails.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    here is what i would do

    void main()
    {
    int i, heads, tails, rannum,;
    srand(time(NULL);
    for (i = 0; i < numberofcointosses)
    {
    num = rand() % 2;
    if (num == 0)
    heads++;
    else
    tails++;
    }
    printf("Percentage of heads = %d", (heads / numberofcointosses));
    printf("Percentage of tails = %d", (tails/ numberofcointosses));
    }

  5. #5
    kidkash
    Guest
    Thanks!

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    8
    Originally posted by sayword
    here is what i would do

    void main()
    {
    int i, heads, tails, rannum,;
    srand(time(NULL);
    for (i = 0; i < numberofcointosses)
    {
    num = rand() % 2;
    if (num == 0)
    heads++;
    else
    tails++;
    }
    printf("Percentage of heads = %d", (heads / numberofcointosses));
    printf("Percentage of tails = %d", (tails/ numberofcointosses));
    }
    You would get 0 for percentage if you use integer.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    int main please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 10-24-2008, 01:22 PM
  4. random numbers
    By h_howee in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2005, 02:56 PM
  5. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM