random_range(3)

NAME
random_range - a set of routines for dealing with integer ranges, and random numbers in a range

SYNOPSIS
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)

DESCRIPTION
This is a set of routines for parsing numeric ranges, and choosing random numbers from a range.

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.

EXAMPLES
#include <stdio.h>

main(argc, argv)
int	argc;
char	**argv;
{
	int		r;
	char		*errp;
	extern void	random_range_seed();
	extern long	random_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);
}

SEE ALSO
lrand48(3c)

DIAGNOSTICS
If random_range() fails, errp will point to NULL, and the return value will be undefined. If mult is 1, there are no possible error conditions, so the return value is always valid in this case.

BUGS
On CRAY systems, random_range(), random_rangel(), random_rangell() all have the 64 bit limit since int, long and long long are always 64 bits.

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.