1RANDOM(3)                  Library Functions Manual                  RANDOM(3)
2
3
4

NAME

6       random,  srandom, initstate, setstate - better random number generator;
7       routines for changing generators
8

SYNOPSIS

10       long random()
11
12       srandom(seed)
13       int seed;
14
15       char *initstate(seed, state, n)
16       unsigned seed;
17       char *state;
18       int n;
19
20       char *setstate(state)
21       char *state;
22

DESCRIPTION

24       Random uses a non-linear  additive  feedback  random  number  generator
25       employing a default table of size 31 long integers to return successive
26       pseudo-random numbers in the range from 0 to (2**31)−1.  The period  of
27       this   random   number   generator   is   very   large,   approximately
28       16*((2**31)−1).
29
30       Random/srandom have (almost) the same calling sequence and  initializa‐
31       tion properties as rand/srand.  The difference is that rand(3) produces
32       a much less random sequence — in fact, the low dozen bits generated  by
33       rand go through a cyclic pattern.  All the bits generated by random are
34       usable.  For example, ``random()&01''  will  produce  a  random  binary
35       value.
36
37       Unlike srand, srandom does not return the old seed; the reason for this
38       is that the amount of state information used is much more than a single
39       word.   (Two other routines are provided to deal with restarting/chang‐
40       ing random number generators).  Like rand(3), however, random  will  by
41       default produce a sequence of numbers that can be duplicated by calling
42       srandom with 1 as the seed.
43
44       The initstate routine allows a state array, passed in as  an  argument,
45       to  be  initialized  for  future  use.  The size of the state array (in
46       bytes) is used by initstate to decide how sophisticated a random number
47       generator  it  should use -- the more state, the better the random num‐
48       bers will be.  (Current "optimal" values for the amount of state infor‐
49       mation are 8, 32, 64, 128, and 256 bytes; other amounts will be rounded
50       down to the nearest known amount.  Using less than 8 bytes  will  cause
51       an error).  The seed for the initialization (which specifies a starting
52       point for the random number sequence, and provides  for  restarting  at
53       the  same  point)  is also an argument.  Initstate returns a pointer to
54       the previous state information array.
55
56       Once a state has been initialized, the setstate  routine  provides  for
57       rapid switching between states.  Setstate returns a pointer to the pre‐
58       vious state array; its argument state array is used for further  random
59       number generation until the next call to initstate or setstate.
60
61       Once  a state array has been initialized, it may be restarted at a dif‐
62       ferent point either by calling initstate (with the  desired  seed,  the
63       state  array, and its size) or by calling both setstate (with the state
64       array) and srandom (with the desired seed).  The advantage  of  calling
65       both  setstate and srandom is that the size of the state array does not
66       have to be remembered after it is initialized.
67
68       With 256 bytes of state information, the period of  the  random  number
69       generator  is  greater  than  2**69 which should be sufficient for most
70       purposes.
71

AUTHOR

73       Earl T. Cohen
74

DIAGNOSTICS

76       If initstate is called with less than 8 bytes of state information,  or
77       if  setstate detects that the state information has been garbled, error
78       messages are printed on the standard error output.
79

SEE ALSO

81       rand(3)
82

BUGS

84       About 2/3 the speed of rand(3C).
85
86
87
884.2 Berkeley Distribution     September 29, 1985                     RANDOM(3)
Impressum