This code is used to get random numbers with a certain probability:
8 is returned if r
is between 0 and 0.8; 2 is returned if r
is between 0.8 and 1.
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
int main()
{
srand(time(NULL));
double r = rand() / (double)RAND_MAX;
double sum = 8 + 2;
if (r < 8 / sum) {
printf("80% \n");
} else {
printf("20% \n");
}
}
but if I have more than two numbers, say n, how can I handle it? Can I handle it with multiple if-else statements? Or what else?