dimanche 20 janvier 2019

How to generate random 0s and 1s but with 80-20 probability of their occurence in C?

Trying to create a program which generates two random numbers(0 & 1) and stores them in an array and prints them, which i have done successfully, problem is that i need the number 1 to be generated with an 80% probability and the number 0 with a 20% probability.

Already done populating the array with random 1s and 0s with rand()%10 as the random number generated is between 0-10 the logic i used is that if the random number is greater than 5 ,store it in the array as '1' and if less than 5 ,store in the array as '0'

for(i=0;i<=n_gen;i++)               // for allele array
{
     randallele[i]=rand()%10 +1;
     if(randallele[i]>=5)
     {
         randallele[i]=1;
     }
     else
     {
         randallele[i]=0;
     }

}
for(i=0;i<=pop_s;i++)           //prints allele array
{
    printf("Printing the alleles:    %d\n", randallele[i]);
}

I expect the output to be generated along with their probabilities(80% for '1' and 20% for '0') rather than directly storing random 1s and 0s

Thanks




Aucun commentaire:

Enregistrer un commentaire