random(3) CLIX random(3)
NAME
random, srandom - Provides a better random number generator
LIBRARY
Berkeley Software Distribution Library (libbsd.a)
SYNOPSIS
long random(
void );
void srandom(
int seed );
PARAMETERS
seed An integer used to seed the random number generator
DESCRIPTION
The random() function uses a nonlinear additive feedback random number
generator. The random() function employs a default table size of 31 long
integers to return pseudo-random numbers in the range from 0 to 2^31-1.
The period of this random number generator is approximately 16(2^31-1).
The random() and srandom() functions have (almost) the same calling
sequence and initialization properties as rand() and srand(). The
difference is that rand() produces a much less random sequence. In fact,
the low dozen bits generated by rand() go through a cyclic pattern. All
the bits generated by random() are usable. For example,
random() & 01
will produce a random binary value. Unlike srand(), srandom() does not
return the old seed because the amount of state information used is much
more than a single word. Like rand(), however, random() will by default
produce a sequence of numbers that can be duplicated by calling srandom()
with 1 as the seed.
With 256 bytes of state information, the period of the random number
generator is greater than 2^69, which should be sufficient for most
purposes.
EXAMPLES
To create an array of 1000 random numbers using a seed of 16:
#include <stdio.h>
#include <math.h>
2/94 - Intergraph Corporation 1
random(3) CLIX random(3)
main()
{
extern long random();
extern void srandom();
int random_array[1000];
int i;
srandom(16);
for(i = 0; i <= 1000; i++) {
random_array[i] = random();
printf("%d\n", random_array[i]);
}
}
NOTES
The random() function executes at approximately the speed of rand().
To use the random() and srandom() functions, you must compile programs
with the -lbsd flag to the cc command.
RETURN VALUES
The random() function and return pseudo-random numbers in the range from 0
to 2^31-1.
RELATED INFORMATION
Functions: rand(3)
2 Intergraph Corporation - 2/94