NOTE:
These are a little sketchy...I'll fill in details 'soon'.
long rand_L(long min, long max)
{
return rand()%(max-min+1) + min;
}
long rand_L(long min, long max)
{
return (RAND_MAX==LONG_MAX ? rand()
: (long)rand()<<16 | rand())
%(max-min+1) + min;
}
// note: the L on the 0 and 10000 makes sure those literals
// are of type long
double rand_D(double min, double max)
{
return rand_L(0L, 10000L)/10000.0 * (max-min) + min;
}
// note: simply call long integer version by type-casting.
// return character by reversing the type-cast.
char rand_C(char min, char max)
{
return (char)rand_L((long)min, (long)max);
}
// note: notice the L's again and that the range of decimals
// has been extended to 4 for better overall performance
// (not efficiency, but usefulness to an application)
int event_occured(double probability)
{
return rand_L(0L, 10000L) <= probability*10000;
}
int flip_coin(const double * chance_heads) // fair coin
{
double chance = (chance_heads == NULL) ? 0.5 : *chance_heads;
return event_occured(chance);
}