1rand(3C) Standard C Library Functions rand(3C)
2
3
4
6 rand, srand, rand_r - simple random-number generator
7
9 #include <stdlib.h>
10
11 int rand(void);
12
13
14 void srand(unsigned int seed);
15
16
17 int rand_r(unsigned int *seed);
18
19
21 The rand() function uses a multiplicative congruential random-number
22 generator with period 2^32 that returns successive pseudo-random num‐
23 bers in the range of 0 to RAND_MAX (defined in <stdlib.h>).
24
25
26 The srand() function uses the argument seed as a seed for a new
27 sequence of pseudo-random numbers to be returned by subsequent calls to
28 rand(). If srand() is then called with the same seed value, the
29 sequence of pseudo-random numbers will be repeated. If rand() is
30 called before any calls to srand() have been made, the same sequence
31 will be generated as when srand() is first called with a seed value of
32 1.
33
34
35 The rand_r() function has the same functionality as rand() except that
36 a pointer to a seed seed must be supplied by the caller. If rand_r()
37 is called with the same initial value for the object pointed to by seed
38 and that object is not modified between successive calls to rand_r(),
39 the same sequence as that produced by calls to rand() will be gener‐
40 ated.
41
42
43 The rand() and srand() functions provide per-process pseudo-random
44 streams shared by all threads. The same effect can be achieved if all
45 threads call rand_r() with a pointer to the same seed object. The
46 rand_r() function allows a thread to generate a private pseudo-random
47 stream by having the seed object be private to the thread.
48
50 The spectral properties of rand() are limited. The drand48(3C) func‐
51 tion provides a better, more elaborate random-number generator.
52
53
54 When compiling multithreaded applications, the _REENTRANT flag must be
55 defined on the compile line. This flag should be used only in multi‐
56 threaded applications.
57
59 See attributes(5) for descriptions of the following attributes:
60
61
62
63
64 ┌─────────────────────────────┬─────────────────────────────┐
65 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
66 ├─────────────────────────────┼─────────────────────────────┤
67 │Interface Stability │Standard │
68 ├─────────────────────────────┼─────────────────────────────┤
69 │MT-Level │Safe │
70 └─────────────────────────────┴─────────────────────────────┘
71
73 drand48(3C), attributes(5), standards(5)
74
75
76
77SunOS 5.11 19 May 2004 rand(3C)