1RAND(P)                    POSIX Programmer's Manual                   RAND(P)
2
3
4

NAME

6       rand, rand_r, srand - pseudo-random number generator
7

SYNOPSIS

9       #include <stdlib.h>
10
11       int rand(void);
12
13
14       int rand_r(unsigned *seed);
15       void srand(unsigned seed);
16
17

DESCRIPTION

19       The  rand() function shall compute a sequence of pseudo-random integers
20       in the range [0, {RAND_MAX}]    with a period of at least 2**32.
21
22       The rand() function need not be  reentrant.  A  function  that  is  not
23       required to be reentrant is not required to be thread-safe.
24
25       The  rand_r()  function shall compute a sequence of pseudo-random inte‐
26       gers in the range [0, {RAND_MAX}]. (The value of the  {RAND_MAX}  macro
27       shall be at least 32767.)
28
29       If  rand_r()  is  called  with  the  same  initial value for the object
30       pointed to by seed and that object is not modified  between  successive
31       returns and calls to rand_r(), the same sequence shall be generated.
32
33       The  srand() function uses the argument as a seed for a new sequence of
34       pseudo-random numbers to be returned by subsequent calls to rand().  If
35       srand()  is  then  called  with  the  same  seed value, the sequence of
36       pseudo-random numbers shall be repeated. If rand() is called before any
37       calls to srand() are made, the same sequence shall be generated as when
38       srand() is first called with a seed value of 1.
39
40       The implementation shall behave as if no function defined in this  vol‐
41       ume of IEEE Std 1003.1-2001 calls rand() or srand().
42

RETURN VALUE

44       The  rand()  function shall return the next pseudo-random number in the
45       sequence.
46
47       The rand_r() function shall return a pseudo-random integer.
48
49       The srand() function shall not return a value.
50

ERRORS

52       No errors are defined.
53
54       The following sections are informative.
55

EXAMPLES

57   Generating a Pseudo-Random Number Sequence
58       The following example  demonstrates  how  to  generate  a  sequence  of
59       pseudo-random numbers.
60
61
62              #include <stdio.h>
63              #include <stdlib.h>
64              ...
65                  long count, i;
66                  char *keystr;
67                  int elementlen, len;
68                  char c;
69              ...
70              /* Initial random number generator. */
71                  srand(1);
72
73
74                  /* Create keys using only lowercase characters */
75                  len = 0;
76                  for (i=0; i<count; i++) {
77                      while (len < elementlen) {
78                          c = (char) (rand() % 128);
79                          if (islower(c))
80                              keystr[len++] = c;
81                      }
82
83
84                      keystr[len] = '\0';
85                      printf("%s Element%0*ld\n", keystr, elementlen, i);
86                      len = 0;
87                  }
88
89   Generating the Same Sequence on Different Machines
90       The  following  code defines a pair of functions that could be incorpo‐
91       rated into applications wishing to ensure that  the  same  sequence  of
92       numbers is generated across different machines.
93
94
95              static unsigned long next = 1;
96              int myrand(void)  /* RAND_MAX assumed to be 32767. */
97              {
98                  next = next * 1103515245 + 12345;
99                  return((unsigned)(next/65536) % 32768);
100              }
101
102
103              void mysrand(unsigned seed)
104              {
105                  next = seed;
106              }
107

APPLICATION USAGE

109       The  drand48()  function  provides  a much more elaborate random number
110       generator.
111
112       The limitations on the amount of state that can be carried between  one
113       function  call  and  another  mean  the  rand_r() function can never be
114       implemented in a way which satisfies  all  of  the  requirements  on  a
115       pseudo-random  number  generator.  Therefore  this  function  should be
116       avoided whenever non-trivial requirements (including safety) have to be
117       fulfilled.
118

RATIONALE

120       The  ISO C  standard  rand()  and  srand()  functions allow per-process
121       pseudo-random streams shared by all threads. Those two  functions  need
122       not  change,  but there has to be mutual-exclusion that prevents inter‐
123       ference between two threads concurrently accessing  the  random  number
124       generator.
125
126       With  regard  to  rand(), there are two different behaviors that may be
127       wanted in a multi-threaded program:
128
129        1. A single per-process sequence  of  pseudo-random  numbers  that  is
130           shared by all threads that call rand()
131
132        2. A  different sequence of pseudo-random numbers for each thread that
133           calls rand()
134
135       This is provided by the modified thread-safe function based on  whether
136       the seed value is global to the entire process or local to each thread.
137
138       This  does  not  address  the known deficiencies of the rand() function
139       implementations, which have been approached by maintaining more  state.
140       In  effect,  this  specifies new thread-safe forms of a deficient func‐
141       tion.
142

FUTURE DIRECTIONS

144       None.
145

SEE ALSO

147       drand48()  ,  the  Base  Definitions  volume  of  IEEE Std 1003.1-2001,
148       <stdlib.h>
149
151       Portions  of  this text are reprinted and reproduced in electronic form
152       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
153       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
154       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
155       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
156       event of any discrepancy between this version and the original IEEE and
157       The  Open Group Standard, the original IEEE and The Open Group Standard
158       is the referee document. The original Standard can be  obtained  online
159       at http://www.opengroup.org/unix/online.html .
160
161
162
163IEEE/The Open Group                  2003                              RAND(P)
Impressum