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

1. ## 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. 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_tail = (nr_of_tail / nr_of_tosses) * 100;

3. 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. here is what i would do

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

5. Thanks!

6. Originally posted by sayword
here is what i would do

void main()
{
srand(time(NULL);
for (i = 0; i < numberofcointosses)
{
num = rand() % 2;
if (num == 0)