void random_range_seed(int seed)
long random_range(int min, int max, int mult, char **errp)
long random_rangel(long min, long max, long mult, char **errp)
long long random_rangell(long long min, long long max,
long long mult, char **errp)
long random_bit(long mask)
random_range() chooses a random number in the range min-max (inclusive) which is a multiple of mult. min and max may be any integer, but mult must be a positive integer greater than 0. errp is a char ** which is used to detect error conditions. If errp is not NULL, *errp will be set to point to an error message. If errp is NULL, error conditions cannot be detected by the caller. If mult is 1 (the most common case), there are no possible error conditions, and the return value is guaranteed to be valid.
random_range_seed() sets the random number generator seed to the specified value.
random_bit() will return a randomly selected single bit bitmask from the bits set in mask. The bit is randomly chosen using random_range(). If mask is zero, zero is returned.
random_range() functions uses lrand48() internally. If the range is bigger than will fit in a 32 bit long (2G), lrand48() with a a internal recursive algorithm to produce a random number.
#include <stdio.h>
main(argc, argv)
int argc;
char **argv;
{
int r;
char *errp;
extern voidrandom_range_seed();
extern longrandom_range();
random_range_seed(getpid());
r = random_range(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), &errp);
if (errp == NULL) {
fprintf(stderr, "random_range failed: %s, errp);
exit(1);
} else {
printf("%d, r);
}
exit(0);
}
On IRIX systems, random_range() can only produce a 32 number. random_rangel() when compiled as a 32 bit object is still limited to 32 bit number. random_rangell() can be used to return a value bigger than 32 bits even when compiled as a 32 bit object.