1 .\" $NetBSD: random.3,v 1.4 1995/12/28 08:52:42 thorpej Exp $
3 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
4 .\" 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. All advertising materials mentioning features or use of this software
15 .\" must display the following acknowledgement:
16 .\" This product includes software developed by the University of
17 .\" California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" from: @(#)random.3 6.5 (Berkeley) 4/19/91
44 .Nd better random number generator; routines for changing generators
46 .Fd #include <stdlib.h>
50 .Fn srandom "unsigned seed"
52 .Fn initstate "unsigned seed" "char *state" "int n"
54 .Fn setstate "char *state"
59 uses a non-linear additive feedback random number generator employing a
60 default table of size 31 long integers to return successive pseudo-random
61 numbers in the range from 0 to
62 .if t 2\u\s731\s10\d\(mi1.
64 The period of this random number generator is very large, approximately
65 .if t 16\(mu(2\u\s731\s10\d\(mi1).
66 .if n 16*((2**31)\(mi1).
69 .Fn random Ns / Fn srandom
70 have (almost) the same calling sequence and initialization properties as
71 .Xr rand 3 Ns / Xr srand 3 .
72 The difference is that
74 produces a much less random sequence \(em in fact, the low dozen bits
75 generated by rand go through a cyclic pattern. All the bits generated by
77 are usable. For example,
79 will produce a random binary
85 does not return the old seed; the reason for this is that the amount of
86 state information used is much more than a single word. (Two other
87 routines are provided to deal with restarting/changing random
88 number generators). Like
92 will by default produce a sequence of numbers that can be duplicated
101 routine allows a state array, passed in as an argument, to be initialized
102 for future use. The size of the state array (in bytes) is used by
104 to decide how sophisticated a random number generator it should use \(em the
105 more state, the better the random numbers will be.
106 (Current "optimal" values for the amount of state information are
107 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
108 the nearest known amount. Using less than 8 bytes will cause an error.)
109 The seed for the initialization (which specifies a starting point for
110 the random number sequence, and provides for restarting at the same
111 point) is also an argument.
115 returns a pointer to the previous state information array.
117 Once a state has been initialized, the
119 routine provides for rapid switching between states.
123 returns a pointer to the previous state array; its
124 argument state array is used for further random number generation
125 until the next call to
130 Once a state array has been initialized, it may be restarted at a
131 different point either by calling
133 (with the desired seed, the state array, and its size) or by calling
136 (with the state array) and
138 (with the desired seed).
139 The advantage of calling both
143 is that the size of the state array does not have to be remembered after
146 With 256 bytes of state information, the period of the random number
147 generator is greater than
148 .if t 2\u\s769\s10\d,
150 which should be sufficient for most purposes.
156 is called with less than 8 bytes of state information, or if
158 detects that the state information has been garbled, error
159 messages are printed on the standard error output.
164 functions appeared in
167 About 2/3 the speed of