1RANDOM(3)                  Linux Programmer's Manual                 RANDOM(3)
2
3
4

NAME

6       random, srandom, initstate, setstate - random number generator
7

SYNOPSIS

9       #include <stdlib.h>
10
11       long int random(void);
12       void srandom(unsigned int seed);
13       char *initstate(unsigned int seed, char *state, size_t n);
14       char *setstate(char *state);
15

DESCRIPTION

17       The random() function uses a non-linear additive feedback random number
18       generator employing a default table of size 31 long integers to  return
19       successive  pseudo-random numbers in the range from 0 to RAND_MAX.  The
20       period of this random number generator  is  very  large,  approximately
21       16*((2**31)-1).
22
23       The srandom() function sets its argument as the seed for a new sequence
24       of pseudo-random integers to be returned by random().  These  sequences
25       are  repeatable  by  calling srandom() with the same seed value.  If no
26       seed value is provided, the random() function is  automatically  seeded
27       with a value of 1.
28
29       The  initstate()  function allows a state array state to be initialized
30       for use by random().  The size of the state array n is  used  by  init‐
31       state() to decide how sophisticated a random number generator it should
32       use — the larger the state array, the better the  random  numbers  will
33       be.   seed is the seed for the initialization, which specifies a start‐
34       ing point for the random number sequence, and provides  for  restarting
35       at the same point.
36
37       The  setstate()  function  changes the state array used by the random()
38       function.  The state array state is used for random  number  generation
39       until  the  next  call  to initstate() or setstate().  state must first
40       have been initialized using initstate() or be the result of a  previous
41       call of setstate().
42

RETURN VALUE

44       The  random()  function  returns  a  value between 0 and RAND_MAX.  The
45       srandom() function returns no value.  The  initstate()  and  setstate()
46       functions  return  a  pointer  to  the previous state array, or NULL on
47       error.
48

ERRORS

50       EINVAL A state array of less than 8 bytes was specified to initstate().
51

NOTES

53       Current "optimal" values for the size of the state array n are  8,  32,
54       64, 128, and 256 bytes; other amounts will be rounded down to the near‐
55       est known amount.  Using less than 8 bytes will cause an error.
56

CONFORMING TO

58       4.3BSD, POSIX.1-2001.
59

SEE ALSO

61       rand(3), srand(3)
62
63
64
65GNU                               2000-08-20                         RANDOM(3)
Impressum