1DRAND48(3P) POSIX Programmer's Manual DRAND48(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11 delim $$
12
14 drand48, erand48, jrand48, lcong48, lrand48, mrand48, nrand48, seed48,
15 srand48 — generate uniformly distributed pseudo-random numbers
16
18 #include <stdlib.h>
19
20 double drand48(void);
21 double erand48(unsigned short xsubi[3]);
22 long jrand48(unsigned short xsubi[3]);
23 void lcong48(unsigned short param[7]);
24 long lrand48(void);
25 long mrand48(void);
26 long nrand48(unsigned short xsubi[3]);
27 unsigned short *seed48(unsigned short seed16v[3]);
28 void srand48(long seedval);
29
31 This family of functions shall generate pseudo-random numbers using a
32 linear congruential algorithm and 48-bit integer arithmetic.
33
34 The drand48() and erand48() functions shall return non-negative, dou‐
35 ble-precision, floating-point values, uniformly distributed over the
36 interval [0.0,1.0).
37
38 The lrand48() and nrand48() functions shall return non-negative, long
39 integers, uniformly distributed over the interval [0,231).
40
41 The mrand48() and jrand48() functions shall return signed long integers
42 uniformly distributed over the interval [−231,231).
43
44 The srand48(), seed48(), and lcong48() functions are initialization
45 entry points, one of which should be invoked before either drand48(),
46 lrand48(), or mrand48() is called. (Although it is not recommended
47 practice, constant default initializer values shall be supplied auto‐
48 matically if drand48(), lrand48(), or mrand48() is called without a
49 prior call to an initialization entry point.) The erand48(), nrand48(),
50 and jrand48() functions do not require an initialization entry point to
51 be called first.
52
53 All the routines work by generating a sequence of 48-bit integer val‐
54 ues, $X_ i" " ,$ according to the linear congruential formula:
55
56 $X sub{n+1} " " = " " (aX_ n" "^+^c) sub{roman mod " " m} " " "
57 " " " " " " " " " " " " " n>= " " 0$
58
59 The parameter $m^=^2"^" 48$; hence 48-bit integer arithmetic is per‐
60 formed. Unless lcong48() is invoked, the multiplier value $a$ and the
61 addend value $c$ are given by:
62
63 $a " " mark = " " roman "5DEECE66D"^sub 16 " " = " " roman
64 273673163155^sub 8$
65
66 $c " " lineup = " " roman B^sub 16 " " = " " roman 13^sub 8$
67
68 The value returned by any of the drand48(), erand48(), jrand48(),
69 lrand48(), mrand48(), or nrand48() functions is computed by first gen‐
70 erating the next 48-bit $X_ i$ in the sequence. Then the appropriate
71 number of bits, according to the type of data item to be returned, are
72 copied from the high-order (leftmost) bits of $X_ i$ and transformed
73 into the returned value.
74
75 The drand48(), lrand48(), and mrand48() functions store the last 48-bit
76 $X_ i$ generated in an internal buffer; that is why the application
77 shall ensure that these are initialized prior to being invoked. The
78 erand48(), nrand48(), and jrand48() functions require the calling pro‐
79 gram to provide storage for the successive $X_ i$ values in the array
80 specified as an argument when the functions are invoked. That is why
81 these routines do not have to be initialized; the calling program
82 merely has to place the desired initial value of $X_ i$ into the array
83 and pass it as an argument. By using different arguments, erand48(),
84 nrand48(), and jrand48() allow separate modules of a large program to
85 generate several independent streams of pseudo-random numbers; that is,
86 the sequence of numbers in each stream shall not depend upon how many
87 times the routines are called to generate numbers for the other
88 streams.
89
90 The initializer function srand48() sets the high-order 32 bits of $X_
91 i$ to the low-order 32 bits contained in its argument. The low-order 16
92 bits of $X_ i$ are set to the arbitrary value $roman 330E_ 16" " .$
93
94 The initializer function seed48() sets the value of $X_ i$ to the
95 48-bit value specified in the argument array. The low-order 16 bits of
96 $X_ i$ are set to the low-order 16 bits of seed16v[0]. The mid-order
97 16 bits of $X_ i$ are set to the low-order 16 bits of seed16v[1]. The
98 high-order 16 bits of $X_ i$ are set to the low-order 16 bits of
99 seed16v[2]. In addition, the previous value of $X_ i$ is copied into a
100 48-bit internal buffer, used only by seed48(), and a pointer to this
101 buffer is the value returned by seed48(). This returned pointer, which
102 can just be ignored if not needed, is useful if a program is to be
103 restarted from a given point at some future time—use the pointer to get
104 at and store the last $X_ i$ value, and then use this value to reini‐
105 tialize via seed48() when the program is restarted.
106
107 The initializer function lcong48() allows the user to specify the ini‐
108 tial $X_ i" " ,$ the multiplier value $a,$ and the addend value $c.$
109 Argument array elements param[0-2] specify $X_ i" " ,$ param[3-5] spec‐
110 ify the multiplier $a,$ and param[6] specifies the 16-bit addend $c.$
111 After lcong48() is called, a subsequent call to either srand48() or
112 seed48() shall restore the standard multiplier and addend values, a and
113 c, specified above.
114
115 The drand48(), lrand48(), and mrand48() functions need not be thread-
116 safe.
117
119 As described in the DESCRIPTION above.
120
122 No errors are defined.
123
124 The following sections are informative.
125
127 None.
128
130 None.
131
133 None.
134
136 None.
137
139 rand()
140
141 The Base Definitions volume of POSIX.1‐2008, <stdlib.h>
142
144 Portions of this text are reprinted and reproduced in electronic form
145 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
146 -- Portable Operating System Interface (POSIX), The Open Group Base
147 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
148 cal and Electronics Engineers, Inc and The Open Group. (This is
149 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
150 event of any discrepancy between this version and the original IEEE and
151 The Open Group Standard, the original IEEE and The Open Group Standard
152 is the referee document. The original Standard can be obtained online
153 at http://www.unix.org/online.html .
154
155 Any typographical or formatting errors that appear in this page are
156 most likely to have been introduced during the conversion of the source
157 files to man page format. To report such errors, see https://www.ker‐
158 nel.org/doc/man-pages/reporting_bugs.html .
159
160
161
162IEEE/The Open Group 2013 DRAND48(3P)