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