NOTE:
These are a little sketchy...I'll fill in details 'soon'.
random whole number between min and max inclusive: [min..max]
rand() % (max - min + 1) + min
inline long rand_range(long min, long max) { return rand()%(max-min+1) + min; }
But why's that still not quite right? It is a long and tedious story involving bits and sub-standard standard libraries and all manner of nauseum. For more on the details, you can look at this page for more details on the issues involved, but essentially, they recommend something more like this:
inline long rand_range(long min, long max) { return rand()/(RAND_MAX / (max-min+1) + 1) + min; }
random decimal values between min and max inclusive: [min, max]
rand_range(0,1000)/1000.0 * (max - min) + min
rand_range(0,RAND_MAX-1)/(RAND_MAX-1.0) * (max - min) + min
Note that the L on the 0 and 1 makes sure those literals are of type long so that the compiler can tell which rand_range function to call.
inline double rand_range(double min, double max) { return rand_range(0L, RAND_MAX-1L)/(RAND_MAX-1.0) * (max-min) + min; }
Of particular use, in fact:
inline double rand_01(void) { return rand_range(0L, RAND_MAX-1L)/(RAND_MAX-1.0); }
which makes the other one cleaner:
inline double rand_range(double min, double max) { return rand_01() * (max-min) + min; }
random character values between min and max inclusive: [min..max]
code = rand_range(min,max)
Note that we simply call the long integer version via type-casting and then return the character by reversing the type-cast.
inline char rand_range(char min, char max) { return static_cast<char>(rand_range(static_cast<long>(min), static_cast<long>(max))); }
Did an action known to have a P percent chance of occurring happen or not?
rand_range(0,100) <= P*100
Or better:
// probability is expected to be a standard [0, 1] value inline bool event_occurred(double probability) { return rand_01() <= probability; }
Most commonly:
inline bool flip_coin(double chance_heads = 0.5) // fair coin { return event_occurred(chance_heads); }