N random choice according to probability
this code is to get random numbers but with probability : that 8 is
returned if r= 0 to 0.8 and 2 is returned if r= 0.8 to 0.2
#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 multi if else statement or what ?
No comments:
Post a Comment