1RAND(3P) POSIX Programmer's Manual RAND(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
12 rand, rand_r, srand - pseudo-random number generator
13
15 #include <stdlib.h>
16
17 int rand(void);
18
19
20 int rand_r(unsigned *seed);
21 void srand(unsigned seed);
22
23
25 The rand() function shall compute a sequence of pseudo-random integers
26 in the range [0, {RAND_MAX}] with a period of at least 2**32.
27
28 The rand() function need not be reentrant. A function that is not
29 required to be reentrant is not required to be thread-safe.
30
31 The rand_r() function shall compute a sequence of pseudo-random inte‐
32 gers in the range [0, {RAND_MAX}]. (The value of the {RAND_MAX} macro
33 shall be at least 32767.)
34
35 If rand_r() is called with the same initial value for the object
36 pointed to by seed and that object is not modified between successive
37 returns and calls to rand_r(), the same sequence shall be generated.
38
39 The srand() function uses the argument as a seed for a new sequence of
40 pseudo-random numbers to be returned by subsequent calls to rand(). If
41 srand() is then called with the same seed value, the sequence of
42 pseudo-random numbers shall be repeated. If rand() is called before any
43 calls to srand() are made, the same sequence shall be generated as when
44 srand() is first called with a seed value of 1.
45
46 The implementation shall behave as if no function defined in this vol‐
47 ume of IEEE Std 1003.1-2001 calls rand() or srand().
48
50 The rand() function shall return the next pseudo-random number in the
51 sequence.
52
53 The rand_r() function shall return a pseudo-random integer.
54
55 The srand() function shall not return a value.
56
58 No errors are defined.
59
60 The following sections are informative.
61
63 Generating a Pseudo-Random Number Sequence
64 The following example demonstrates how to generate a sequence of
65 pseudo-random numbers.
66
67
68 #include <stdio.h>
69 #include <stdlib.h>
70 ...
71 long count, i;
72 char *keystr;
73 int elementlen, len;
74 char c;
75 ...
76 /* Initial random number generator. */
77 srand(1);
78
79
80 /* Create keys using only lowercase characters */
81 len = 0;
82 for (i=0; i<count; i++) {
83 while (len < elementlen) {
84 c = (char) (rand() % 128);
85 if (islower(c))
86 keystr[len++] = c;
87 }
88
89
90 keystr[len] = '\0';
91 printf("%s Element%0*ld\n", keystr, elementlen, i);
92 len = 0;
93 }
94
95 Generating the Same Sequence on Different Machines
96 The following code defines a pair of functions that could be incorpo‐
97 rated into applications wishing to ensure that the same sequence of
98 numbers is generated across different machines.
99
100
101 static unsigned long next = 1;
102 int myrand(void) /* RAND_MAX assumed to be 32767. */
103 {
104 next = next * 1103515245 + 12345;
105 return((unsigned)(next/65536) % 32768);
106 }
107
108
109 void mysrand(unsigned seed)
110 {
111 next = seed;
112 }
113
115 The drand48() function provides a much more elaborate random number
116 generator.
117
118 The limitations on the amount of state that can be carried between one
119 function call and another mean the rand_r() function can never be
120 implemented in a way which satisfies all of the requirements on a
121 pseudo-random number generator. Therefore this function should be
122 avoided whenever non-trivial requirements (including safety) have to be
123 fulfilled.
124
126 The ISO C standard rand() and srand() functions allow per-process
127 pseudo-random streams shared by all threads. Those two functions need
128 not change, but there has to be mutual-exclusion that prevents inter‐
129 ference between two threads concurrently accessing the random number
130 generator.
131
132 With regard to rand(), there are two different behaviors that may be
133 wanted in a multi-threaded program:
134
135 1. A single per-process sequence of pseudo-random numbers that is
136 shared by all threads that call rand()
137
138 2. A different sequence of pseudo-random numbers for each thread that
139 calls rand()
140
141 This is provided by the modified thread-safe function based on whether
142 the seed value is global to the entire process or local to each thread.
143
144 This does not address the known deficiencies of the rand() function
145 implementations, which have been approached by maintaining more state.
146 In effect, this specifies new thread-safe forms of a deficient func‐
147 tion.
148
150 None.
151
153 drand48(), the Base Definitions volume of IEEE Std 1003.1-2001,
154 <stdlib.h>
155
157 Portions of this text are reprinted and reproduced in electronic form
158 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
159 -- Portable Operating System Interface (POSIX), The Open Group Base
160 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
161 Electrical and Electronics Engineers, Inc and The Open Group. In the
162 event of any discrepancy between this version and the original IEEE and
163 The Open Group Standard, the original IEEE and The Open Group Standard
164 is the referee document. The original Standard can be obtained online
165 at http://www.opengroup.org/unix/online.html .
166
167
168
169IEEE/The Open Group 2003 RAND(3P)