1 .\" Copyright (c) 1983, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by the University of
15 .\" California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)random.3 8.1 (Berkeley) 6/4/93
33 .\" $FreeBSD: src/lib/libc/stdlib/random.3,v 1.20 2004/07/02 23:52:12 ru Exp $
44 .Nd better random number generator; routines for changing generators
61 .Fa "const char *state"
75 uses a non-linear, additive feedback, random number generator, employing a
76 default table of size 31 long integers.
77 It returns successive pseudo-random
78 numbers in the range from 0 to
79 .if t 2\u\s731\s10\d\(mi1.
81 The period of this random number generator is very large, approximately
82 .if t 16\(mu(2\u\s731\s10\d\(mi1).
83 .if n 16*((2**31)\(mi1).
89 functions have (almost) the same calling sequence and initialization properties as the
94 The difference is that
96 produces a much less random sequence \(em in fact, the low dozen bits
97 generated by rand go through a cyclic pattern.
98 All of the bits generated by
103 will produce a random binary
109 will by default produce a sequence of numbers that can be duplicated
118 routine initializes a state array, using the
120 random number device which returns good random numbers,
121 suitable for cryptographic use.
122 Note that this particular seeding
123 procedure can generate states which are impossible to reproduce by
126 with any value, since the succeeding terms in the
127 state buffer are no longer derived from the LC algorithm applied to
132 routine allows a state array, passed in as an argument, to be initialized
134 The size of the state array (in bytes) is used by
136 to decide how sophisticated a random number generator it should use \(em the
137 more state, the better the random numbers will be.
138 (Current "optimal" values for the amount of state information are
139 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
140 the nearest known amount.
141 Using less than 8 bytes will cause an error.)
142 The seed for the initialization (which specifies a starting point for
143 the random number sequence and provides for restarting at the same
144 point) is also an argument.
148 returns a pointer to the previous state information array.
150 Once a state has been initialized, the
152 routine provides for rapid switching between states.
156 returns a pointer to the previous state array; its
157 argument state array is used for further random number generation
158 until the next call to
163 Once a state array has been initialized, it may be restarted at a
164 different point either by calling
166 (with the desired seed, the state array, and its size) or by calling
169 (with the state array) and
171 (with the desired seed).
172 The advantage of calling both
176 is that the size of the state array does not have to be remembered after
179 With 256 bytes of state information, the period of the random number
180 generator is greater than
181 .if t 2\u\s769\s10\d,
183 which should be sufficient for most purposes.
189 is called with less than 8 bytes of state information, or if
191 detects that the state information has been garbled, error
192 messages are printed on the standard error output.
194 .Fd #include <stdlib.h>
199 .Fa "unsigned long seed"
213 .Fa "unsigned long seed"
216 The type of each parameter is different in the legacy version.
225 functions appeared in
228 About 2/3 the speed of
231 The historical implementation used to have a very weak seeding; the
232 random sequence did not vary much with the seed.
233 The current implementation employs a better pseudo-random number
234 generator for the initial state calculation.
236 Applications requiring cryptographic quality randomness should use