BSD 4_2 development
[unix-history] / usr / man / man3 / random.3
CommitLineData
5436e645
C
1.TH RANDOM 3 "19 January 1983"
2.UC 4
3.SH NAME
4random, srandom, initstate, setstate \- better random number generator; routines for changing generators
5.SH SYNOPSIS
6.nf
7.B long random()
8.PP
9.B srandom(seed)
10.B int seed;
11.PP
12.B char *initstate(seed, state, n)
13.B unsigned seed;
14.B char *state;
15.B int n;
16.PP
17.B char *setstate(state)
18.B char *state;
19.fi
20.SH DESCRIPTION
21.PP
22.I Random
23uses a non-linear additive feedback random number generator employing a
24default table of size 31 long integers to return successive pseudo-random
25numbers in the range from 0 to 2\u\s731\s10\d\-1. The period of this
26random number generator is very large, approximately 16*(2\u\s731\s10\d\-1).
27.PP
28.I Random/srandom
29have (almost) the same calling sequence and initialization properties as
30.I rand/srand.
31The difference is that
32.IR rand (3)
33produces a much less random sequence -- in fact, the low dozen bits
34generated by rand go through a cyclic pattern. All the bits generated by
35.I random
36are usable. For example, \*(lqrandom()&01\*(rq will produce a random binary
37value.
38.PP
39Unlike
40.IR srand ,
41.I srandom
42does not return the old seed; the reason for this is that the amount of
43state information used is much more than a single word. (Two other
44routines are provided to deal with restarting/changing random
45number generators). Like
46.IR rand (3),
47however,
48.I random
49will by default produce a sequence of numbers that can be duplicated
50by calling
51.I srandom
52with
53.I 1
54as the seed.
55.PP
56The
57.I initstate
58routine allows a state array, passed in as an argument, to be initialized
59for future use. The size of the state array (in bytes) is used by
60.I initstate
61to decide how sophisticated a random number generator it should use -- the
62more state, the better the random numbers will be.
63(Current "optimal" values for the amount of state information are
648, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
65the nearest known amount. Using less than 8 bytes will cause an error).
66The seed for the initialization (which specifies a starting point for
67the random number sequence, and provides for restarting at the same
68point) is also an argument.
69.I Initstate
70returns a pointer to the previous state information array.
71.PP
72Once a state has been initialized, the
73.I setstate
74routine provides for rapid switching between states.
75.I Setstate returns a pointer to the previous state array; its
76argument state array is used for further random number generation
77until the next call to
78.I initstate
79or
80.I setstate.
81.PP
82Once a state array has been initialized, it may be restarted at a
83different point either by calling
84.I initstate
85(with the desired seed, the state array, and its size) or by calling
86both
87.I setstate
88(with the state array) and
89.I srandom
90(with the desired seed).
91The advantage of calling both
92.I setstate
93and
94.I srandom
95is that the size of the state array does not have to be remembered after
96it is initialized.
97.PP
98With 256 bytes of state information, the period of the random number
99generator is greater than 2\u\s769\s10\d, which should be sufficient for
100most purposes.
101.SH AUTHOR
102Earl T. Cohen
103.SH DIAGNOSTICS
104.PP
105If
106.I initstate
107is called with less than 8 bytes of state information, or if
108.I setstate
109detects that the state information has been garbled, error
110messages are printed on the standard error output.
111.SH "SEE ALSO"
112rand(3)
113.SH BUGS
114About 2/3 the speed of
115.IR rand (3C).