]> git.saurik.com Git - apple/libc.git/blame - stdlib/FreeBSD/random.3
Libc-1081.1.3.tar.gz
[apple/libc.git] / stdlib / FreeBSD / random.3
CommitLineData
5b2abdfb
A
1.\" Copyright (c) 1983, 1991, 1993
2.\" The Regents of the University of California. All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
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.
5b2abdfb
A
12.\" 4. Neither the name of the University nor the names of its contributors
13.\" may be used to endorse or promote products derived from this software
14.\" without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\" @(#)random.3 8.1 (Berkeley) 6/4/93
1f2f436a 29.\" $FreeBSD: src/lib/libc/stdlib/random.3,v 1.22 2007/01/09 00:28:10 imp Exp $
5b2abdfb
A
30.\"
31.Dd June 4, 1993
32.Dt RANDOM 3
33.Os
34.Sh NAME
ad3c9f2a 35.Nm initstate ,
5b2abdfb 36.Nm random ,
ad3c9f2a 37.Nm setstate ,
5b2abdfb 38.Nm srandom ,
ad3c9f2a 39.Nm srandomdev
5b2abdfb
A
40.Nd better random number generator; routines for changing generators
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.In stdlib.h
ad3c9f2a
A
45.Ft char *
46.Fo initstate
47.Fa "unsigned seed"
48.Fa "char *state"
49.Fa "size_t size"
50.Fc
5b2abdfb 51.Ft long
ad3c9f2a
A
52.Fo random
53.Fa void
54.Fc
55.Ft char *
56.Fo setstate
57.Fa "const char *state"
58.Fc
5b2abdfb 59.Ft void
ad3c9f2a
A
60.Fo srandom
61.Fa "unsigned seed"
62.Fc
5b2abdfb 63.Ft void
ad3c9f2a
A
64.Fo srandomdev
65.Fa void
66.Fc
5b2abdfb
A
67.Sh DESCRIPTION
68The
69.Fn random
70function
ad3c9f2a
A
71uses a non-linear, additive feedback, random number generator, employing a
72default table of size 31 long integers.
73It returns successive pseudo-random
5b2abdfb
A
74numbers in the range from 0 to
75.if t 2\u\s731\s10\d\(mi1.
76.if n (2**31)\(mi1.
77The period of this random number generator is very large, approximately
78.if t 16\(mu(2\u\s731\s10\d\(mi1).
79.if n 16*((2**31)\(mi1).
80.Pp
81The
82.Fn random
83and
84.Fn srandom
85functions have (almost) the same calling sequence and initialization properties as the
86.Xr rand 3
87and
88.Xr srand 3
89functions.
90The difference is that
91.Xr rand 3
92produces a much less random sequence \(em in fact, the low dozen bits
3d9156a7 93generated by rand go through a cyclic pattern.
ad3c9f2a 94All of the bits generated by
5b2abdfb 95.Fn random
3d9156a7
A
96are usable.
97For example,
5b2abdfb
A
98.Sq Li random()&01
99will produce a random binary
100value.
101.Pp
102Like
ad3c9f2a
A
103.Xr srand 3 ,
104.Fn srandom
105sets the initial seed value for future calls to
106.Fn random .
107Like
5b2abdfb
A
108.Xr rand 3 ,
109.Fn random
110will by default produce a sequence of numbers that can be duplicated
111by calling
112.Fn srandom
ad3c9f2a 113with the same seed.
5b2abdfb
A
114.Pp
115The
116.Fn srandomdev
ad3c9f2a 117routine initializes a state array, using the
5b2abdfb
A
118.Xr random 4
119random number device which returns good random numbers,
120suitable for cryptographic use.
121Note that this particular seeding
122procedure can generate states which are impossible to reproduce by
123calling
124.Fn srandom
125with any value, since the succeeding terms in the
126state buffer are no longer derived from the LC algorithm applied to
127a fixed seed.
128.Pp
129The
130.Fn initstate
131routine allows a state array, passed in as an argument, to be initialized
3d9156a7
A
132for future use.
133The size of the state array (in bytes) is used by
5b2abdfb
A
134.Fn initstate
135to decide how sophisticated a random number generator it should use \(em the
136more state, the better the random numbers will be.
137(Current "optimal" values for the amount of state information are
1388, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
3d9156a7
A
139the nearest known amount.
140Using less than 8 bytes will cause an error.)
5b2abdfb 141The seed for the initialization (which specifies a starting point for
ad3c9f2a 142the random number sequence and provides for restarting at the same
5b2abdfb
A
143point) is also an argument.
144The
145.Fn initstate
146function
147returns a pointer to the previous state information array.
148.Pp
149Once a state has been initialized, the
150.Fn setstate
151routine provides for rapid switching between states.
152The
153.Fn setstate
154function
155returns a pointer to the previous state array; its
156argument state array is used for further random number generation
157until the next call to
158.Fn initstate
159or
160.Fn setstate .
161.Pp
162Once a state array has been initialized, it may be restarted at a
163different point either by calling
164.Fn initstate
165(with the desired seed, the state array, and its size) or by calling
166both
167.Fn setstate
168(with the state array) and
169.Fn srandom
170(with the desired seed).
171The advantage of calling both
172.Fn setstate
173and
174.Fn srandom
175is that the size of the state array does not have to be remembered after
176it is initialized.
177.Pp
178With 256 bytes of state information, the period of the random number
179generator is greater than
180.if t 2\u\s769\s10\d,
ad3c9f2a 181.if n 2**69 ,
5b2abdfb 182which should be sufficient for most purposes.
5b2abdfb
A
183.Sh DIAGNOSTICS
184If
185.Fn initstate
186is called with less than 8 bytes of state information, or if
187.Fn setstate
188detects that the state information has been garbled, error
189messages are printed on the standard error output.
ad3c9f2a
A
190.Sh LEGACY SYNOPSIS
191.Fd #include <stdlib.h>
192.Pp
193.Ft char *
194.br
195.Fo initstate
196.Fa "unsigned long seed"
197.Fa "char *state"
198.Fa "long size"
199.Fc ;
200.Pp
201.Ft char *
202.br
203.Fo setstate
204.Fa "char *state"
205.Fc ;
206.Pp
207.Ft void
208.br
209.Fo srandom
210.Fa "unsigned long seed"
211.Fc ;
212.Pp
213The type of each parameter is different in the legacy version.
5b2abdfb 214.Sh SEE ALSO
3d9156a7 215.Xr arc4random 3 ,
5b2abdfb
A
216.Xr rand 3 ,
217.Xr srand 3 ,
ad3c9f2a
A
218.Xr random 4 ,
219.Xr compat 5
5b2abdfb
A
220.Sh HISTORY
221These
222functions appeared in
223.Bx 4.2 .
1f2f436a
A
224.Sh AUTHORS
225.An Earl T. Cohen
5b2abdfb
A
226.Sh BUGS
227About 2/3 the speed of
228.Xr rand 3 .
229.Pp
230The historical implementation used to have a very weak seeding; the
231random sequence did not vary much with the seed.
232The current implementation employs a better pseudo-random number
233generator for the initial state calculation.
3d9156a7
A
234.Pp
235Applications requiring cryptographic quality randomness should use
236.Xr arc4random 3 .