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