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