xref: /original-bsd/lib/libc/stdlib/random.3 (revision 698bcc85)
1.\" Copyright (c) 1983, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" %sccs.include.redist.man%
5.\"
6.\"     @(#)random.3	6.5 (Berkeley) 04/19/91
7.\"
8.Dd
9.Dt RANDOM 3
10.Os BSD 4.2
11.Sh NAME
12.Nm random ,
13.Nm srandom ,
14.Nm initstate ,
15.Nm setstate
16.Nd better random number generator; routines for changing generators
17.Sh SYNOPSIS
18.Fd #include <stdlib>
19.Ft long
20.Fn random void
21.Ft void
22.Fn srandom "unsigned seed"
23.Ft char *
24.Fn initstate "unsigned seed" "char *state" "int n"
25.Ft char *
26.Fn setstate "char *state"
27.Sh DESCRIPTION
28The
29.Fn random
30function
31uses a non-linear additive feedback random number generator employing a
32default table of size 31 long integers to return successive pseudo-random
33numbers in the range from 0 to
34.if t 2\u\s731\s10\d\(mi1.
35.if n (2**31)\(mi1.
36The period of this random number generator is very large, approximately
37.if t 16\(mu(2\u\s731\s10\d\(mi1).
38.if n 16*((2**31)\(mi1).
39.Pp
40The
41.Fn random Ns / Fn srandom
42have (almost) the same calling sequence and initialization properties as
43.Xr rand 3 Ns / Xr srand 3 .
44The difference is that
45.Xr rand
46produces a much less random sequence \(em in fact, the low dozen bits
47generated by rand go through a cyclic pattern.  All the bits generated by
48.Fn random
49are usable.  For example,
50.Sq Li random()&01
51will produce a random binary
52value.
53.Pp
54Unlike
55.Xr srand ,
56.Fn srandom
57does not return the old seed; the reason for this is that the amount of
58state information used is much more than a single word.  (Two other
59routines are provided to deal with restarting/changing random
60number generators).  Like
61.Xr rand 3 ,
62however,
63.Fn random
64will by default produce a sequence of numbers that can be duplicated
65by calling
66.Fn srandom
67with
68.Ql 1
69as the seed.
70.Pp
71The
72.Fn initstate
73routine allows a state array, passed in as an argument, to be initialized
74for future use.  The size of the state array (in bytes) is used by
75.Fn initstate
76to decide how sophisticated a random number generator it should use \(em the
77more state, the better the random numbers will be.
78(Current "optimal" values for the amount of state information are
798, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
80the nearest known amount.  Using less than 8 bytes will cause an error.)
81The seed for the initialization (which specifies a starting point for
82the random number sequence, and provides for restarting at the same
83point) is also an argument.
84The
85.Fn initstate
86function
87returns a pointer to the previous state information array.
88.Pp
89Once a state has been initialized, the
90.Fn setstate
91routine provides for rapid switching between states.
92The
93.Fn setstate
94function
95returns a pointer to the previous state array; its
96argument state array is used for further random number generation
97until the next call to
98.Fn initstate
99or
100.Fn setstate .
101.Pp
102Once a state array has been initialized, it may be restarted at a
103different point either by calling
104.Fn initstate
105(with the desired seed, the state array, and its size) or by calling
106both
107.Fn setstate
108(with the state array) and
109.Fn srandom
110(with the desired seed).
111The advantage of calling both
112.Fn setstate
113and
114.Fn srandom
115is that the size of the state array does not have to be remembered after
116it is initialized.
117.Pp
118With 256 bytes of state information, the period of the random number
119generator is greater than
120.if t 2\u\s769\s10\d,
121.if n 2**69
122which should be sufficient for most purposes.
123.Sh AUTHOR
124Earl T. Cohen
125.Sh DIAGNOSTICS
126If
127.Fn initstate
128is called with less than 8 bytes of state information, or if
129.Fn setstate
130detects that the state information has been garbled, error
131messages are printed on the standard error output.
132.Sh SEE ALSO
133.Xr rand 3
134.Sh HISTORY
135These
136functions appeared in
137.Bx 4.2 .
138.Sh BUGS
139About 2/3 the speed of
140.Xr rand 3 .
141