1 .\" $NetBSD: random.3,v 1.18 2003/08/07 16:43:43 agc Exp $
3 .\" Copyright (c) 1983, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" from: @(#)random.3 8.1 (Berkeley) 6/4/93
40 .Nd better random number generator; routines for changing generators
48 .Fn srandom "unsigned long seed"
50 .Fn initstate "unsigned long seed" "char *state" "size_t n"
52 .Fn setstate "char *state"
57 uses a non-linear additive feedback random number generator employing a
58 default table of size 31 long integers to return successive pseudo-random
59 numbers in the range from 0 to
60 .if t 2\u\s731\s10\d\(mi1.
62 The period of this random number generator is very large, approximately
63 .if t 16\(mu(2\u\s731\s10\d\(mi1).
64 .if n 16*((2**31)\(mi1).
74 have (almost) the same calling sequence and initialization properties as
78 The difference is that
80 produces a much less random sequence \(em in fact, the low dozen bits
83 go through a cyclic pattern.
84 All the bits generated by
88 .Sq Li random()\*[Am]01
89 will produce a random binary value.
94 will by default produce a sequence of numbers that can be duplicated
103 routine allows a state array, passed in as an argument, to be initialized
105 The size of the state array (in bytes) is used by
107 to decide how sophisticated a random number generator it should use \(em the
108 more state, the better the random numbers will be.
109 (Current "optimal" values for the amount of state information are
110 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
111 the nearest known amount.
112 Using less than 8 bytes will cause an error).
113 The seed for the initialization (which specifies a starting point for
114 the random number sequence, and provides for restarting at the same
115 point) is also an argument.
116 The state array passed to
118 must be aligned to a 32-bit boundary.
119 This can be achieved by using
120 a suitably-sized array of ints, and casting the array to char * when
126 returns a pointer to the previous state information array.
128 Once a state has been initialized, the
130 routine provides for rapid switching between states.
134 returns a pointer to the previous state array; its
135 argument state array is used for further random number generation
136 until the next call to
141 Once a state array has been initialized, it may be restarted at a
142 different point either by calling
144 (with the desired seed, the state array, and its size) or by calling
147 (with the state array) and
149 (with the desired seed).
150 The advantage of calling both
154 is that the size of the state array does not have to be remembered after
157 With 256 bytes of state information, the period of the random number
158 generator is greater than
159 .if t 2\u\s769\s10\d,
161 which should be sufficient for most purposes.
165 is called with less than 8 bytes of state information, or if
167 detects that the state information has been garbled, error
168 messages are printed on the standard error output.
176 functions appeared in
181 About 2/3 the speed of