1RAND(3)                    Linux Programmer's Manual                   RAND(3)
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       int rand_r(unsigned int *seedp);
14
15       void srand(unsigned int seed);
16
17   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19       rand_r(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
20

DESCRIPTION

22       The  rand()  function  returns  a  pseudo-random  integer  in the range
23       [0, RAND_MAX].
24
25       The srand() function sets its argument as the seed for a  new  sequence
26       of  pseudo-random  integers  to be returned by rand().  These sequences
27       are repeatable by calling srand() with the same seed value.
28
29       If no seed value is provided,  the  rand()  function  is  automatically
30       seeded with a value of 1.
31
32       The function rand() is not reentrant or thread-safe, since it uses hid‐
33       den state that is modified on each call.  This might just be  the  seed
34       value to be used by the next call, or it might be something more elabo‐
35       rate.  In order to get reproducible behavior in a threaded application,
36       this  state  must  be made explicit.  The function rand_r() is supplied
37       with a pointer to an unsigned int, to be used as state.  This is a very
38       small  amount  of  state, so this function will be a weak pseudo-random
39       generator.  Try drand48_r(3) instead.
40

RETURN VALUE

42       The rand()  and  rand_r()  functions  return  a  value  between  0  and
43       RAND_MAX.  The srand() function returns no value.
44

CONFORMING TO

46       The  functions  rand()  and  srand() conform to SVr4, 4.3BSD, C89, C99,
47       POSIX.1-2001.    The   function   rand_r()   is   from    POSIX.1-2001.
48       POSIX.1-2008 marks rand_r() as obsolete.
49

NOTES

51       The  versions of rand() and srand() in the Linux C Library use the same
52       random number generator as random(3) and srandom(3), so the lower-order
53       bits  should  be as random as the higher-order bits.  However, on older
54       rand() implementations, and on  current  implementations  on  different
55       systems,  the  lower-order  bits  are much less random than the higher-
56       order bits.  Do not use this function in applications  intended  to  be
57       portable when good randomness is needed.  (Use random(3) instead.)
58

EXAMPLE

60       POSIX.1-2001 gives the following example of an implementation of rand()
61       and srand(), possibly useful when one needs the same  sequence  on  two
62       different machines.
63
64           static unsigned long next = 1;
65
66           /* RAND_MAX assumed to be 32767 */
67           int myrand(void) {
68               next = next * 1103515245 + 12345;
69               return((unsigned)(next/65536) % 32768);
70           }
71
72           void mysrand(unsigned seed) {
73               next = seed;
74           }
75

SEE ALSO

77       drand48(3), random(3)
78

COLOPHON

80       This  page  is  part of release 3.25 of the Linux man-pages project.  A
81       description of the project, and information about reporting  bugs,  can
82       be found at http://www.kernel.org/doc/man-pages/.
83
84
85
86                                  2008-08-29                           RAND(3)
Impressum