1random(3C)               Standard C Library Functions               random(3C)
2
3
4

NAME

6       random, srandom, initstate, setstate - pseudorandom number functions
7

SYNOPSIS

9       #include <stdlib.h>
10
11       long random(void);
12
13
14       void srandom(unsigned int seed);
15
16
17       char *initstate(unsigned int seed, char *state, size_t size);
18
19
20       char *setstate(const char *state);
21
22

DESCRIPTION

24       The  random() function uses a nonlinear additive feedback random-number
25       generator employing a default state array size of 31 long  integers  to
26       return successive pseudo-random numbers in the range from 0 to 2^31 −1.
27       The period of this random-number generator is approximately 16 x  (2^31
28       −1).  The  size of the state array determines the period of the random-
29       number generator. Increasing the state array size increases the period.
30
31
32       The srandom() function initializes the current state  array  using  the
33       value of seed.
34
35
36       The  random()  and  srandom()  functions have (almost) the same calling
37       sequence and initialization  properties  as  rand()  and  srand()  (see
38       rand(3C)).  The difference is that rand(3C) produces a much less random
39       sequence—in fact, the low dozen bits generated by  rand  go  through  a
40       cyclic pattern. All the bits generated by random() are usable.
41
42
43       The algorithm from rand() is used by srandom() to generate the 31 state
44       integers. Because of this, different  srandom()  seeds  often  produce,
45       within an offset, the same sequence of low order bits from random(). If
46       low order bits are used directly, random() should be  initialized  with
47       setstate() using high quality random values.
48
49
50       Unlike  srand(),  srandom()  does  not  return the old seed because the
51       amount of state information used is much more than a single  word.  Two
52       other  routines  are  provided  to deal with restarting/changing random
53       number generators. With 256 bytes of state information, the  period  of
54       the  random-number generator is greater than 2^69, which should be suf‐
55       ficient for most purposes.
56
57
58       Like rand(3C), random() produces by default a sequence of numbers  that
59       can be duplicated by calling srandom() with 1 as the seed.
60
61
62       The initstate() and setstate() functions handle restarting and changing
63       random-number generators.  The  initstate()  function  allows  a  state
64       array,  pointed  to by the state argument, to be initialized for future
65       use. The size argument, which specifies the size in bytes of the  state
66       array, is used by initstate() to decide what type of random-number gen‐
67       erator to use; the larger the state array, the more random the numbers.
68       Values  for the amount of state information are 8, 32, 64, 128, and 256
69       bytes.  Other values greater than 8 bytes are rounded down to the near‐
70       est  one  of  these values.  For values smaller than 8, random() uses a
71       simple linear congruential random number generator.  The seed  argument
72       specifies  a starting point for the random-number sequence and provides
73       for restarting at the same point.  The initstate() function  returns  a
74       pointer to the previous state information array.
75
76
77       If  initstate()  has  not  been called, then random() behaves as though
78       initstate() had been called with seed=1 and size=128.
79
80
81       If initstate() is called with size<8, then random() uses a simple  lin‐
82       ear congruential random number generator.
83
84
85       Once  a state has been initialized, setstate() allows switching between
86       state arrays. The array defined by the state argument is used for  fur‐
87       ther random-number generation until initstate() is called or setstate()
88       is called again. The setstate() function returns a pointer to the  pre‐
89       vious state array.
90

RETURN VALUES

92       The random() function returns the generated pseudo-random number.
93
94
95       The srandom() function returns no value.
96
97
98       Upon successful completion, initstate() and setstate() return a pointer
99       to the previous state array.  Otherwise, a null pointer is returned.
100

ERRORS

102       No errors are defined.
103

USAGE

105       After initialization, a state array can be  restarted  at  a  different
106       point in one of two ways:
107
108           o      The initstate() function can be used, with the desired seed,
109                  state array, and size of the array.
110
111           o      The setstate() function, with  the  desired  state,  can  be
112                  used,  followed  by  srandom()  with  the  desired seed. The
113                  advantage of using both of these functions is that the  size
114                  of the state array does not have to be saved once it is ini‐
115                  tialized.
116

EXAMPLES

118       Example 1 Initialize an array.
119
120
121       The following example demonstrates the use of initstate() to  intialize
122       an  array.  It also demonstrates how to initialize an array and pass it
123       to setstate().
124
125
126         # include <stdlib.h>
127         static unsigned int state0[32];
128         static unsigned int state1[32] = {
129              3,
130              0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342,
131              0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd,
132              0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86,
133              0xda672e2a, 0x1588ca88, 0xe369735d, 0x904f35f7,
134              0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc,
135              0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb,
136              0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b,
137              0xf5ad9d0e, 0x8999220b, 0x27fb47b9
138              };
139         main() {
140              unsigned seed;
141              int n;
142              seed = 1;
143              n = 128;
144              (void)initstate(seed, (char *)state0, n);
145              printf("random() = %d0\n", random());
146              (void)setstate((char *)state1);
147              printf("random() = %d0\n", random());
148         }
149
150

ATTRIBUTES

152       See attributes(5) for descriptions of the following attributes:
153
154
155
156
157       ┌─────────────────────────────┬─────────────────────────────┐
158       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
159       ├─────────────────────────────┼─────────────────────────────┤
160       │Interface Stability          │Standard                     │
161       ├─────────────────────────────┼─────────────────────────────┤
162       │MT-Level                     │See NOTES below.             │
163       └─────────────────────────────┴─────────────────────────────┘
164

SEE ALSO

166       drand48(3C), rand(3C), attributes(5), standards(5)
167

NOTES

169       The random() and srandom() functions are unsafe in multithreaded appli‐
170       cations.
171
172
173       Use of these functions in multithreaded applications is unsupported.
174
175
176       For  initstate()  and setstate(), the state argument must be aligned on
177       an int boundary.
178
179
180       Newer and better performing random number generators such as  addrans()
181       and lcrans() are available with the SUNWspro package.
182
183
184
185SunOS 5.11                        14 Aug 2002                       random(3C)
Impressum