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