1 .\" Copyright (c) 1983 Regents of the University of California.
2 .\" All rights reserved. The Berkeley software License Agreement
3 .\" specifies the terms and conditions for redistribution.
5 .\" @(#)random.3 6.2 (Berkeley) 9/29/85
7 .TH RANDOM 3 "September 29, 1985"
10 random, srandom, initstate, setstate \- better random number generator; routines for changing generators
17 void srandom(unsigned \fIseed\fP)
18 char *initstate(unsigned \fIseed\fP, char *\fIstate\fP, int \fIn\fP)
19 char *setstate(char *\fIstate\fP)
25 uses a non-linear additive feedback random number generator employing a
26 default table of size 31 long integers to return successive pseudo-random
27 numbers in the range from 0 to
28 .if t 2\u\s731\s10\d\(mi1.
30 The period of this random number generator is very large, approximately
31 .if t 16\(mu(2\u\s731\s10\d\(mi1).
32 .if n 16*((2**31)\(mi1).
35 have (almost) the same calling sequence and initialization properties as
37 The difference is that
39 produces a much less random sequence \(em in fact, the low dozen bits
40 generated by rand go through a cyclic pattern. All the bits generated by
42 are usable. For example, ``random()&01'' will produce a random binary
48 does not return the old seed; the reason for this is that the amount of
49 state information used is much more than a single word. (Two other
50 routines are provided to deal with restarting/changing random
51 number generators). Like
55 will by default produce a sequence of numbers that can be duplicated
64 routine allows a state array, passed in as an argument, to be initialized
65 for future use. The size of the state array (in bytes) is used by
67 to decide how sophisticated a random number generator it should use -- the
68 more state, the better the random numbers will be.
69 (Current "optimal" values for the amount of state information are
70 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
71 the nearest known amount. Using less than 8 bytes will cause an error).
72 The seed for the initialization (which specifies a starting point for
73 the random number sequence, and provides for restarting at the same
74 point) is also an argument.
76 returns a pointer to the previous state information array.
78 Once a state has been initialized, the
80 routine provides for rapid switching between states.
82 returns a pointer to the previous state array; its
83 argument state array is used for further random number generation
84 until the next call to
89 Once a state array has been initialized, it may be restarted at a
90 different point either by calling
92 (with the desired seed, the state array, and its size) or by calling
95 (with the state array) and
97 (with the desired seed).
98 The advantage of calling both
102 is that the size of the state array does not have to be remembered after
105 With 256 bytes of state information, the period of the random number
106 generator is greater than
107 .if t 2\u\s769\s10\d,
109 which should be sufficient for most purposes.
116 is called with less than 8 bytes of state information, or if
118 detects that the state information has been garbled, error
119 messages are printed on the standard error output.
128 programmers must provide their own declarations.
130 About 2/3 the speed of