3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
6 * This code is a modified version of the module GB_FLIP, a portable
7 * pseudo-random number generator. The original version of GB_FLIP is
8 * a part of The Stanford GraphBase developed by Donald E. Knuth (see
9 * http://www-cs-staff.stanford.edu/~knuth/sgb.html).
11 * Note that all changes concern only external names, so this modified
12 * version produces exactly the same results as the original version.
14 * Changes were made by Andrew Makhorin <mao@gnu.org>.
16 * GLPK is free software: you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * GLPK is distributed in the hope that it will be useful, but WITHOUT
22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
24 * License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
28 ***********************************************************************/
38 /* pseudo-random values */
43 #define fptr (rand->fptr)
45 /* the next A value to be exported */
47 #define mod_diff(x, y) (((x) - (y)) & 0x7FFFFFFF)
48 /* difference modulo 2^31 */
50 static int flip_cycle(RNG *rand)
51 { /* this is an auxiliary routine to do 55 more steps of the basic
52 recurrence, at high speed, and to reset fptr */
54 for (ii = &A[1], jj = &A[32]; jj <= &A[55]; ii++, jj++)
55 *ii = mod_diff(*ii, *jj);
56 for (jj = &A[1]; ii <= &A[55]; ii++, jj++)
57 *ii = mod_diff(*ii, *jj);
62 /***********************************************************************
65 * rng_create_rand - create pseudo-random number generator
70 * RNG *rng_create_rand(void);
74 * The routine rng_create_rand creates and initializes a pseudo-random
79 * The routine returns a pointer to the generator created. */
81 RNG *rng_create_rand(void)
84 rand = xmalloc(sizeof(RNG));
86 for (i = 1; i <= 55; i++) A[i] = 0;
88 rng_init_rand(rand, 1);
92 /***********************************************************************
95 * rng_init_rand - initialize pseudo-random number generator
100 * void rng_init_rand(RNG *rand, int seed);
104 * The routine rng_init_rand initializes the pseudo-random number
105 * generator. The parameter seed may be any integer number. Note that
106 * on creating the generator this routine is called with the parameter
107 * seed equal to 1. */
109 void rng_init_rand(RNG *rand, int seed)
111 int prev = seed, next = 1;
112 seed = prev = mod_diff(prev, 0);
114 for (i = 21; i; i = (i + 21) % 55)
116 next = mod_diff(prev, next);
118 seed = 0x40000000 + (seed >> 1);
121 next = mod_diff(next, seed);
132 /***********************************************************************
135 * rng_next_rand - obtain pseudo-random integer in the range [0, 2^31-1]
139 * #include "glprng.h"
140 * int rng_next_rand(RNG *rand);
144 * The routine rng_next_rand returns a next pseudo-random integer which
145 * is uniformly distributed between 0 and 2^31-1, inclusive. The period
146 * length of the generated numbers is 2^85 - 2^30. The low order bits of
147 * the generated numbers are just as random as the high-order bits. */
149 int rng_next_rand(RNG *rand)
151 *fptr >= 0 ? *fptr-- : flip_cycle(rand);
154 /***********************************************************************
157 * rng_unif_rand - obtain pseudo-random integer in the range [0, m-1]
161 * #include "glprng.h"
162 * int rng_unif_rand(RNG *rand, int m);
166 * The routine rng_unif_rand returns a next pseudo-random integer which
167 * is uniformly distributed between 0 and m-1, inclusive, where m is any
168 * positive integer less than 2^31. */
170 #define two_to_the_31 ((unsigned int)0x80000000)
172 int rng_unif_rand(RNG *rand, int m)
173 { unsigned int t = two_to_the_31 - (two_to_the_31 % m);
176 do { r = rng_next_rand(rand); } while (t <= (unsigned int)r);
180 /***********************************************************************
183 * rng_delete_rand - delete pseudo-random number generator
187 * #include "glprng.h"
188 * void rng_delete_rand(RNG *rand);
192 * The routine rng_delete_rand frees all the memory allocated to the
193 * specified pseudo-random number generator. */
195 void rng_delete_rand(RNG *rand)
200 /**********************************************************************/
203 /* To be sure that this modified version produces the same results as
204 the original version, run this validation program. */
209 rand = rng_create_rand();
210 rng_init_rand(rand, -314159);
211 if (rng_next_rand(rand) != 119318998)
212 { fprintf(stderr, "Failure on the first try!\n");
215 for (j = 1; j <= 133; j++) rng_next_rand(rand);
216 if (rng_unif_rand(rand, 0x55555555) != 748103812)
217 { fprintf(stderr, "Failure on the second try!\n");
220 fprintf(stderr, "OK, the random-number generator routines seem to"
222 rng_delete_rand(rand);