maint: update copyright
[gnulib.git] / lib / random.c
1 /* Family of functions for random integers.
2    Copyright (C) 1995-1997, 2000-2002, 2012-2014 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /*
18  * This is derived from the Berkeley source:
19  *      @(#)random.c    5.5 (Berkeley) 7/6/88
20  * It was reworked for the GNU C Library by Roland McGrath.
21  * Rewritten to use reentrant functions by Ulrich Drepper, 1995.
22  */
23
24 /*
25    Copyright (C) 1983 Regents of the University of California.
26    All rights reserved.
27
28    Redistribution and use in source and binary forms, with or without
29    modification, are permitted provided that the following conditions
30    are met:
31
32    1. Redistributions of source code must retain the above copyright
33       notice, this list of conditions and the following disclaimer.
34    2. Redistributions in binary form must reproduce the above copyright
35       notice, this list of conditions and the following disclaimer in the
36       documentation and/or other materials provided with the distribution.
37    4. Neither the name of the University nor the names of its contributors
38       may be used to endorse or promote products derived from this software
39       without specific prior written permission.
40
41    THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
42    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44    ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
45    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51    SUCH DAMAGE.*/
52
53 #include <config.h>
54
55 /* Specification.  */
56 #include <stdlib.h>
57
58 #include <stdint.h>
59 #include <time.h>
60
61 /* This file can assume the 'struct random_data' and associated *_r functions
62    from gnulib.  */
63
64 /* Linear congruential.  */
65 #define TYPE_0          0
66 #define BREAK_0         8
67 #define DEG_0           0
68 #define SEP_0           0
69
70 /* x**7 + x**3 + 1.  */
71 #define TYPE_1          1
72 #define BREAK_1         32
73 #define DEG_1           7
74 #define SEP_1           3
75
76 /* x**15 + x + 1.  */
77 #define TYPE_2          2
78 #define BREAK_2         64
79 #define DEG_2           15
80 #define SEP_2           1
81
82 /* x**31 + x**3 + 1.  */
83 #define TYPE_3          3
84 #define BREAK_3         128
85 #define DEG_3           31
86 #define SEP_3           3
87
88 /* x**63 + x + 1.  */
89 #define TYPE_4          4
90 #define BREAK_4         256
91 #define DEG_4           63
92 #define SEP_4           1
93
94 /* Array versions of the above information to make code run faster.
95    Relies on fact that TYPE_i == i.  */
96
97 #define MAX_TYPES       5       /* Max number of types above.  */
98
99 /* Initially, everything is set up as if from:
100         initstate(1, randtbl, 128);
101    Note that this initialization takes advantage of the fact that srandom
102    advances the front and rear pointers 10*rand_deg times, and hence the
103    rear pointer which starts at 0 will also end up at zero; thus the zeroth
104    element of the state information, which contains info about the current
105    position of the rear pointer is just
106         (MAX_TYPES * (rptr - state)) + TYPE_3 == TYPE_3.  */
107
108 static int32_t randtbl[DEG_3 + 1] =
109   {
110     TYPE_3,
111
112     -1726662223, 379960547, 1735697613, 1040273694, 1313901226,
113     1627687941, -179304937, -2073333483, 1780058412, -1989503057,
114     -615974602, 344556628, 939512070, -1249116260, 1507946756,
115     -812545463, 154635395, 1388815473, -1926676823, 525320961,
116     -1009028674, 968117788, -123449607, 1284210865, 435012392,
117     -2017506339, -911064859, -370259173, 1132637927, 1398500161,
118     -205601318,
119   };
120
121 static struct random_data generator =
122   {
123 /* FPTR and RPTR are two pointers into the state info, a front and a rear
124    pointer.  These two pointers are always rand_sep places aparts, as they
125    cycle through the state information.  (Yes, this does mean we could get
126    away with just one pointer, but the code for random is more efficient
127    this way).  The pointers are left positioned as they would be from the call:
128         initstate(1, randtbl, 128);
129    (The position of the rear pointer, rptr, is really 0 (as explained above
130    in the initialization of randtbl) because the state table pointer is set
131    to point to randtbl[1] (as explained below).)  */
132
133     /* fptr */ &randtbl[SEP_3 + 1],
134     /* rptr */ &randtbl[1],
135
136 /* The following things are the pointer to the state information table,
137    the type of the current generator, the degree of the current polynomial
138    being used, and the separation between the two pointers.
139    Note that for efficiency of random, we remember the first location of
140    the state information, not the zeroth.  Hence it is valid to access
141    state[-1], which is used to store the type of the R.N.G.
142    Also, we remember the last location, since this is more efficient than
143    indexing every time to find the address of the last element to see if
144    the front and rear pointers have wrapped.  */
145
146     /* state */ &randtbl[1],
147
148     /* rand_type */ TYPE_3,
149     /* rand_deg */ DEG_3,
150     /* rand_sep */ SEP_3,
151
152     /* end_ptr */ &randtbl[sizeof (randtbl) / sizeof (randtbl[0])]
153 };
154
155 long
156 random (void)
157 {
158   int32_t val;
159
160   if (random_r (&generator, &val) < 0)
161     abort (); /* should not happen */
162   return val;
163 }
164
165 void
166 srandom (unsigned int seed)
167 {
168   (void) srandom_r (seed, &generator); /* may fail! */
169 }
170
171 char *
172 initstate (unsigned int seed, char *buf, size_t buf_size)
173 {
174   char *old_state = (char *) ((int32_t *) generator.state - 1);
175   if (initstate_r (seed, buf, buf_size, &generator) < 0)
176     return NULL; /* failed */
177   return old_state;
178 }
179
180 char *
181 setstate (char *arg_state)
182 {
183   char *old_state = (char *) ((int32_t *) generator.state - 1);
184   if (setstate_r (arg_state, &generator) < 0)
185     return NULL; /* failed */
186   return old_state;
187 }