Possible Duplicate:
Random numbers with different probabilities
I have to write a function that will take a parameter p = either 0.25 or 0.5 or 0.75
and return yes / no
with Probablity(yes) = p
. As I have uniform random number generator rand()
my idea is
//representing yes -> '1' and no -> '0'
char myRandom(double d){
char array[4];
if (p(yes) == 0.25) strcpy(arr,"1000");
if (p(yes) == 0.50) strcpy(arr,"1100");
if (p(yes) == 0.75) strcpy(arr,"1110");
return arr[rand(0,4)];
}
I know it is not efficient if there are much more probablities. But other than this is there any wrong in my idea? If there is then where?