Generate a pseudo-random number from the default state
#include <stdlib.h> long random( void );
Use the -l c option to qcc to link against this library. This library is usually included automatically.
This function is in libc.a, but not in libc.so (in order to save space).
The random() function uses a nonlinear additive feedback random-number generator employing a default state array size of 31 long integers to return successive pseudo-random numbers in the range from 0 to 231-1. The period of this random-number generator is approximately 16 × (231-1). The size of the state array determines the period of the random-number generator. Increasing the state array size increases the period.
Use this function in conjunction with the following:
- Initialize the state of the pseudo-random number generator.
- Specify the state of the pseudo-random number generator.
- Set the seed used by the pseudo-random number generator.
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
produces a random binary value.
Unlike srand(), srandom() doesn't return the old seed because the amount of state information used is much more than a single word. The initstate() and setstate() routines are provided to deal with restarting/changing random number generators. With 256 bytes of state information, the period of the random-number generator is greater than 269.
Like rand(), random() produces by default a sequence of numbers that can be duplicated by calling srandom() with 1 as the seed.
If initstate() hasn't been called, random() behaves as though initstate() had been called with seed=1 and size=128.
If initstate() is called with size less than 8, random() uses a simple linear congruential random number generator.
The generated pseudo-random number.
Last modified: 2014-06-24