]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) |
2 | .\" | |
93015253 | 3 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
4 | .\" Permission is granted to make and distribute verbatim copies of this |
5 | .\" manual provided the copyright notice and this permission notice are | |
6 | .\" preserved on all copies. | |
7 | .\" | |
8 | .\" Permission is granted to copy and distribute modified versions of this | |
9 | .\" manual under the conditions for verbatim copying, provided that the | |
10 | .\" entire resulting derived work is distributed under the terms of a | |
11 | .\" permission notice identical to this one. | |
c13182ef | 12 | .\" |
fea681da MK |
13 | .\" Since the Linux kernel and libraries are constantly changing, this |
14 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
15 | .\" responsibility for errors or omissions, or for damages resulting from | |
16 | .\" the use of the information contained herein. The author(s) may not | |
17 | .\" have taken the same level of care in the production of this manual, | |
18 | .\" which is licensed free of charge, as they might when working | |
19 | .\" professionally. | |
c13182ef | 20 | .\" |
fea681da MK |
21 | .\" Formatted or processed versions of this manual, if unaccompanied by |
22 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 23 | .\" %%%LICENSE_END |
fea681da MK |
24 | .\" |
25 | .\" References consulted: | |
26 | .\" Linux libc source code | |
27 | .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991) | |
28 | .\" 386BSD man pages | |
29 | .\" Modified Sat Jul 24 19:46:03 1993 by Rik Faith (faith@cs.unc.edu) | |
4b8c67d9 | 30 | .TH DRAND48 3 2017-09-15 "" "Linux Programmer's Manual" |
fea681da MK |
31 | .SH NAME |
32 | drand48, erand48, lrand48, nrand48, mrand48, jrand48, srand48, seed48, | |
33 | lcong48 \- generate uniformly distributed pseudo-random numbers | |
34 | .SH SYNOPSIS | |
35 | .nf | |
36 | .B #include <stdlib.h> | |
68e4db0a | 37 | .PP |
fea681da | 38 | .B double drand48(void); |
68e4db0a | 39 | .PP |
fea681da | 40 | .BI "double erand48(unsigned short " xsubi [3]); |
68e4db0a | 41 | .PP |
fea681da | 42 | .B long int lrand48(void); |
68e4db0a | 43 | .PP |
fea681da | 44 | .BI "long int nrand48(unsigned short " xsubi [3]); |
68e4db0a | 45 | .PP |
fea681da | 46 | .B long int mrand48(void); |
68e4db0a | 47 | .PP |
fea681da | 48 | .BI "long int jrand48(unsigned short " xsubi [3]); |
68e4db0a | 49 | .PP |
fea681da | 50 | .BI "void srand48(long int " seedval ); |
68e4db0a | 51 | .PP |
fea681da | 52 | .BI "unsigned short *seed48(unsigned short " seed16v [3]); |
68e4db0a | 53 | .PP |
fea681da MK |
54 | .BI "void lcong48(unsigned short " param [7]); |
55 | .fi | |
68e4db0a | 56 | .PP |
cc4615cc MK |
57 | .in -4n |
58 | Feature Test Macro Requirements for glibc (see | |
59 | .BR feature_test_macros (7)): | |
60 | .in | |
68e4db0a | 61 | .PP |
cc4615cc | 62 | .ad l |
cfc2d88d | 63 | All functions shown above: |
cc4615cc MK |
64 | .\" .BR drand48 (), |
65 | .\" .BR erand48 (), | |
66 | .\" .BR lrand48 (), | |
67 | .\" .BR nrand48 (), | |
68 | .\" .BR mrand48 (), | |
69 | .\" .BR jrand48 (), | |
70 | .\" .BR srand48 (), | |
71 | .\" .BR seed48 (), | |
72 | .\" .BR lcong48 (): | |
2b1b0424 MK |
73 | _XOPEN_SOURCE |
74 | || /* Glibc since 2.19: */ _DEFAULT_SOURCE | |
75 | || /* Glibc versions <= 2.19: */ _SVID_SOURCE | |
cc4615cc | 76 | .ad b |
fea681da MK |
77 | .SH DESCRIPTION |
78 | These functions generate pseudo-random numbers using the linear congruential | |
79 | algorithm and 48-bit integer arithmetic. | |
80 | .PP | |
60a90ecd MK |
81 | The |
82 | .BR drand48 () | |
83 | and | |
84 | .BR erand48 () | |
2fda57bd | 85 | functions return nonnegative |
b62ea03a MK |
86 | double-precision floating-point values uniformly distributed over the interval |
87 | [0.0,\ 1.0). | |
fea681da | 88 | .PP |
60a90ecd MK |
89 | The |
90 | .BR lrand48 () | |
91 | and | |
92 | .BR nrand48 () | |
2fda57bd | 93 | functions return nonnegative |
b62ea03a | 94 | long integers uniformly distributed over the interval [0,\ 2^31). |
fea681da | 95 | .PP |
60a90ecd MK |
96 | The |
97 | .BR mrand48 () | |
98 | and | |
99 | .BR jrand48 () | |
100 | functions return signed long | |
b62ea03a | 101 | integers uniformly distributed over the interval [\-2^31,\ 2^31). |
fea681da | 102 | .PP |
60a90ecd MK |
103 | The |
104 | .BR srand48 (), | |
105 | .BR seed48 () | |
106 | and | |
107 | .BR lcong48 () | |
108 | functions are | |
fea681da | 109 | initialization functions, one of which should be called before using |
60a90ecd MK |
110 | .BR drand48 (), |
111 | .BR lrand48 () | |
112 | or | |
113 | .BR mrand48 (). | |
c13182ef | 114 | The functions |
60a90ecd MK |
115 | .BR erand48 (), |
116 | .BR nrand48 () | |
117 | and | |
118 | .BR jrand48 () | |
119 | do not require | |
fea681da MK |
120 | an initialization function to be called first. |
121 | .PP | |
c13182ef | 122 | All the functions work by generating a sequence of 48-bit integers, |
c6fa0841 MK |
123 | .IR Xi , |
124 | according to the linear congruential formula: | |
ba39b288 MK |
125 | .PP |
126 | .in +4n | |
127 | .EX | |
fea681da | 128 | .B Xn+1 = (aXn + c) mod m, where n >= 0 |
ba39b288 MK |
129 | .EE |
130 | .in | |
131 | .PP | |
c6fa0841 MK |
132 | The parameter |
133 | .I m | |
134 | = 2^48, hence 48-bit integer arithmetic is performed. | |
60a90ecd MK |
135 | Unless |
136 | .BR lcong48 () | |
c6fa0841 | 137 | is called, |
51700fd7 | 138 | .IR a |
c6fa0841 MK |
139 | and |
140 | .I c | |
141 | are given by: | |
ba39b288 MK |
142 | .PP |
143 | .in +4n | |
144 | .EX | |
fea681da MK |
145 | .B a = 0x5DEECE66D |
146 | .B c = 0xB | |
ba39b288 MK |
147 | .EE |
148 | .in | |
149 | .PP | |
60a90ecd MK |
150 | The value returned by any of the functions |
151 | .BR drand48 (), | |
152 | .BR erand48 (), | |
153 | .BR lrand48 (), | |
154 | .BR nrand48 (), | |
155 | .BR mrand48 () | |
156 | or | |
157 | .BR jrand48 () | |
158 | is | |
c6fa0841 MK |
159 | computed by first generating the next 48-bit |
160 | .I Xi | |
161 | in the sequence. | |
fea681da | 162 | Then the appropriate number of bits, according to the type of data item to |
c6fa0841 MK |
163 | be returned, is copied from the high-order bits of |
164 | .I Xi | |
165 | and transformed | |
fea681da MK |
166 | into the returned value. |
167 | .PP | |
60a90ecd MK |
168 | The functions |
169 | .BR drand48 (), | |
170 | .BR lrand48 () | |
171 | and | |
172 | .BR mrand48 () | |
173 | store | |
c6fa0841 MK |
174 | the last 48-bit |
175 | .I Xi | |
176 | generated in an internal buffer. | |
c13182ef | 177 | The functions |
60a90ecd MK |
178 | .BR erand48 (), |
179 | .BR nrand48 () | |
180 | and | |
181 | .BR jrand48 () | |
182 | require the calling | |
c6fa0841 MK |
183 | program to provide storage for the successive |
184 | .I Xi | |
185 | values in the array | |
186 | argument | |
187 | .IR xsubi . | |
c13182ef | 188 | The functions are initialized by placing the initial |
c6fa0841 MK |
189 | value of |
190 | .I Xi | |
191 | into the array before calling the function for the first | |
fea681da MK |
192 | time. |
193 | .PP | |
60a90ecd MK |
194 | The initializer function |
195 | .BR srand48 () | |
196 | sets the high order 32-bits of | |
c6fa0841 MK |
197 | .I Xi |
198 | to the argument | |
199 | .IR seedval . | |
c13182ef | 200 | The low order 16-bits are set |
fea681da MK |
201 | to the arbitrary value 0x330E. |
202 | .PP | |
60a90ecd MK |
203 | The initializer function |
204 | .BR seed48 () | |
c6fa0841 MK |
205 | sets the value of |
206 | .I Xi | |
207 | to | |
208 | the 48-bit value specified in the array argument | |
209 | .IR seed16v . | |
c13182ef | 210 | The |
c6fa0841 MK |
211 | previous value of |
212 | .I Xi | |
213 | is copied into an internal buffer and a | |
60a90ecd MK |
214 | pointer to this buffer is returned by |
215 | .BR seed48 (). | |
fea681da | 216 | .PP |
60a90ecd MK |
217 | The initialization function |
218 | .BR lcong48 () | |
219 | allows the user to specify | |
c6fa0841 MK |
220 | initial values for |
221 | .IR Xi , | |
222 | .I a | |
223 | and | |
224 | .IR c . | |
c13182ef | 225 | Array argument |
c6fa0841 | 226 | elements |
9bc87ed0 | 227 | .I param[0\-2] |
c6fa0841 MK |
228 | specify |
229 | .IR Xi , | |
9bc87ed0 | 230 | .I param[3\-5] |
c6fa0841 MK |
231 | specify |
232 | .IR a , | |
51700fd7 | 233 | and |
c6fa0841 MK |
234 | .I param[6] |
235 | specifies | |
236 | .IR c . | |
60a90ecd MK |
237 | After |
238 | .BR lcong48 () | |
239 | has been called, a subsequent call to either | |
240 | .BR srand48 () | |
241 | or | |
242 | .BR seed48 () | |
c6fa0841 MK |
243 | will restore the standard values of |
244 | .I a | |
245 | and | |
246 | .IR c . | |
ba368065 | 247 | .SH ATTRIBUTES |
485507b7 MK |
248 | For an explanation of the terms used in this section, see |
249 | .BR attributes (7). | |
250 | .ad l | |
251 | .TS | |
252 | allbox; | |
253 | lb lb lb | |
d059a452 | 254 | lw21 l lw22. |
485507b7 MK |
255 | Interface Attribute Value |
256 | T{ | |
ba368065 PH |
257 | .BR drand48 (), |
258 | .BR erand48 (), | |
259 | .BR lrand48 (), | |
260 | .BR nrand48 (), | |
261 | .BR mrand48 (), | |
262 | .BR jrand48 (), | |
263 | .BR srand48 (), | |
264 | .BR seed48 (), | |
ba368065 | 265 | .BR lcong48 () |
485507b7 | 266 | T} Thread safety T{ |
c6e2ef68 | 267 | MT-Unsafe race:drand48 |
485507b7 MK |
268 | T} |
269 | .TE | |
270 | .ad | |
271 | .PP | |
272 | The above | |
974afcfe | 273 | functions record global state information for the random number generator, |
ba368065 | 274 | so they are not thread-safe. |
47297adb | 275 | .SH CONFORMING TO |
6c64e4a5 | 276 | POSIX.1-2001, POSIX.1-2008, SVr4. |
47297adb | 277 | .SH SEE ALSO |
fea681da MK |
278 | .BR rand (3), |
279 | .BR random (3) |