xref: /freebsd/lib/libc/stdlib/rand.3 (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. 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.
19.\"
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
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)rand.3	8.1 (Berkeley) 6/4/93
33.\" $FreeBSD$
34.\"
35.Dd February 1, 2020
36.Dt RAND 3
37.Os
38.Sh NAME
39.Nm rand ,
40.Nm srand ,
41.Nm rand_r
42.Nd bad random number generator
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.In stdlib.h
47.Ft void
48.Fn srand "unsigned seed"
49.Ft int
50.Fn rand void
51.Ft int
52.Fn rand_r "unsigned *ctx"
53.Sh DESCRIPTION
54.Bf -symbolic
55The functions described in this manual page are not cryptographically
56secure.
57Applications which require unpredictable random numbers should use
58.Xr arc4random 3
59instead.
60.Ef
61.Pp
62The
63.Fn rand
64function computes a sequence of pseudo-random integers in the range
65of 0 to
66.Dv RAND_MAX ,
67inclusive.
68.Pp
69The
70.Fn srand
71function seeds the algorithm with the
72.Fa seed
73parameter.
74Repeatable sequences of
75.Fn rand
76output may be obtained by calling
77.Fn srand
78with the same
79.Fa seed .
80.Fn rand
81is implicitly initialized as if
82.Fn srand "1"
83had been invoked explicitly.
84.Pp
85In
86.Fx 13 ,
87.Fn rand
88is implemented using the same 128-byte state LFSR generator algorithm as
89.Xr random 3 .
90However, the legacy
91.Fn rand_r
92function is not (and can not be, because of its limited
93.Fa *ctx
94size).
95.Fn rand_r
96implements the historical, poor-quality Park-Miller 32-bit LCG and should not
97be used in new designs.
98.Sh IMPLEMENTATION NOTES
99Since
100.Fx 13 ,
101.Fn rand
102is implemented with the same generator as
103.Xr random 3 ,
104so the low-order bits should no longer be significantly worse than the
105high-order bits.
106.Sh SEE ALSO
107.Xr arc4random 3 ,
108.Xr random 3 ,
109.Xr random 4
110.Sh STANDARDS
111The
112.Fn rand
113and
114.Fn srand
115functions
116conform to
117.St -isoC .
118.Pp
119The
120.Fn rand_r
121function is not part of
122.St -isoC
123and is marked obsolescent in
124.St -p1003.1-2008 .
125It may be removed in a future revision of POSIX.
126.Sh CAVEATS
127Prior to
128.Fx 13 ,
129.Fn rand
130used the historical Park-Miller generator with 32 bits of state and produced
131poor quality output, especially in the lower bits.
132.Fn rand
133in earlier versions of
134.Fx ,
135as well as other standards-conforming implementations, may continue to produce
136poor quality output.
137.Pp
138.Em These functions should not be used in portable applications that want a
139.Em high quality or high performance pseudorandom number generator .
140One possible replacement,
141.Xr random 3 ,
142is portable to Linux — but it is not especially fast, nor standardized.
143.Pp
144If broader portability or better performance is desired, any of the widely
145available and permissively licensed SFC64/32, JSF64/32, PCG64/32, or SplitMix64
146algorithm implementations may be embedded in your application.
147These algorithms have the benefit of requiring less space than
148.Xr random 3
149and being quite fast (in header inline implementations).
150