xref: /freebsd/lib/libc/stdlib/random.3 (revision a4128aad8503277614f2d214011ef60a19447b83)
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.
12.\" 3. 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.Dd February 1, 2020
29.Dt RANDOM 3
30.Os
31.Sh NAME
32.Nm random ,
33.Nm srandom ,
34.Nm srandomdev ,
35.Nm initstate ,
36.Nm setstate
37.Nd non-cryptographic pseudorandom number generator; routines for changing generators
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In stdlib.h
42.Ft long
43.Fn random void
44.Ft void
45.Fn srandom "unsigned int seed"
46.Ft void
47.Fn srandomdev void
48.Ft char *
49.Fn initstate "unsigned int seed" "char *state" "size_t n"
50.Ft char *
51.Fn setstate "char *state"
52.Sh DESCRIPTION
53.Bf -symbolic
54The functions described in this manual page are not secure.
55Applications which require unpredictable random numbers should use
56.Xr arc4random 3
57instead.
58.Ef
59.Pp
60Unless initialized with less than 32 bytes of state, the
61.Fn random
62function
63uses a non-linear additive feedback random number generator employing a
64default table of size 31 long integers to return successive pseudo-random
65numbers in the range from 0 to
66.if t 2\u\s731\s10\d\(mi1.
67.if n (2**31)\(mi1.
68The period of this random number generator is very large, approximately
69.if t 16\(mu(2\u\s731\s10\d\(mi1).
70.if n 16*((2**31)\(mi1).
71.Pp
72If initialized with less than 32 bytes of state,
73.Fn random
74uses the poor-quality 32-bit Park-Miller LCG.
75.Pp
76The
77.Fn random
78and
79.Fn srandom
80functions are analagous to
81.Xr rand 3
82and
83.Xr srand 3 .
84.Pp
85Like
86.Xr rand 3 ,
87.Fn random
88is implicitly initialized as if
89.Fn srandom "1"
90had been invoked explicitly.
91.Pp
92The
93.Fn srandomdev
94routine initializes the state array using random numbers obtained from the
95kernel.
96This can generate states which are impossible to reproduce by calling
97.Fn srandom ,
98because the succeeding terms in the state buffer are no longer derived from the
99Park-Miller LCG algorithm applied to a fixed seed.
100.Pp
101The
102.Fn initstate
103routine initializes the provided state array of
104.Vt uint32_t
105values and uses it in future
106.Fn random
107invocations.
108(Despite the
109.Vt char *
110type of
111.Fa state ,
112the underlying object must be a naturally aligned array of 32-bit values.)
113The size of the state array (in bytes) is used by
114.Fn initstate
115to decide how sophisticated a random number generator it should use \(em the
116more state, the better the random numbers will be.
117(Current "optimal" values for the amount of state information are
1188, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
119the nearest known amount.
120Using less than 8 bytes will cause an error.)
121The
122.Fa seed
123is used as in
124.Fn srandom .
125The
126.Fn initstate
127function
128returns a pointer to the previous state information array.
129.Pp
130The
131.Fn setstate
132routine switches
133.Fn random
134to using the provided state.
135It returns a pointer to the previous state.
136.Pp
137Once a state array has been initialized, it may be restarted at a
138different point either by calling
139.Fn initstate
140(with the desired seed, the state array, and its size) or by calling
141both
142.Fn setstate
143(with the state array) and
144.Fn srandom
145(with the desired seed).
146The advantage of calling both
147.Fn setstate
148and
149.Fn srandom
150is that the size of the state array does not have to be remembered after
151it is initialized.
152.Pp
153With 256 bytes of state information, the period of the random number
154generator is greater than
155.if t 2\u\s769\s10\d,
156.if n 2**69
157which should be sufficient for most purposes.
158.Sh DIAGNOSTICS
159If
160.Fn initstate
161is called with less than 8 bytes of state information, or if
162.Fn setstate
163detects that the state information has been garbled,
164NULL is returned.
165.Sh SEE ALSO
166.Xr arc4random 3 ,
167.Xr lrand48 3 ,
168.Xr rand 3 ,
169.Xr random 4
170.Sh HISTORY
171These
172functions appeared in
173.Bx 4.2 .
174.Sh AUTHORS
175.An Earl T. Cohen
176