2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2008, The GROMACS development team,
6 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012,2013, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
42 #include <gmx_random.h>
51 #ifdef GMX_NATIVE_WINDOWS
56 #include "gmx_random_gausstable.h"
60 #define RNG_MATRIX_A 0x9908b0dfUL /* constant vector a */
61 #define RNG_UPPER_MASK 0x80000000UL /* most significant w-r bits */
62 #define RNG_LOWER_MASK 0x7fffffffUL /* least significant r bits */
64 /* Note that if you change the size of the Gaussian table you will also
65 * have to generate new initialization data for the table in
66 * gmx_random_gausstable.h
68 * A routine print_gaussian_table() is in contrib/random.c
69 * for convenience - use it if you need a different size of the table.
71 #define GAUSS_TABLE 14 /* the size of the gauss table is 2^GAUSS_TABLE */
72 #define GAUSS_SHIFT (32 - GAUSS_TABLE)
76 unsigned int mt[RNG_N];
92 gmx_rng_init(unsigned int seed)
96 if((rng=(struct gmx_rng *)malloc(sizeof(struct gmx_rng)))==NULL)
99 rng->has_saved=0; /* no saved gaussian number yet */
101 rng->mt[0]= seed & 0xffffffffUL;
102 for (rng->mti=1; rng->mti<RNG_N; rng->mti++) {
104 (1812433253UL * (rng->mt[rng->mti-1] ^
105 (rng->mt[rng->mti-1] >> 30)) + rng->mti);
106 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
107 /* In the previous versions, MSBs of the seed affect */
108 /* only MSBs of the array mt[]. */
109 /* 2002/01/09 modified by Makoto Matsumoto */
110 rng->mt[rng->mti] &= 0xffffffffUL;
111 /* for >32 bit machines */
117 gmx_rng_init_array(unsigned int seed[], int seed_length)
122 if((rng=gmx_rng_init(19650218UL))==NULL)
126 k = (RNG_N>seed_length ? RNG_N : seed_length);
128 rng->mt[i] = (rng->mt[i] ^ ((rng->mt[i-1] ^
129 (rng->mt[i-1] >> 30)) * 1664525UL))
130 + seed[j] + j; /* non linear */
131 rng->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
133 if (i>=RNG_N) { rng->mt[0] = rng->mt[RNG_N-1]; i=1; }
134 if (j>=seed_length) j=0;
136 for (k=RNG_N-1; k; k--) {
137 rng->mt[i] = (rng->mt[i] ^ ((rng->mt[i-1] ^
138 (rng->mt[i-1] >> 30)) *
140 - i; /* non linear */
141 rng->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
143 if (i>=RNG_N) { rng->mt[0] = rng->mt[RNG_N-1]; i=1; }
146 rng->mt[0] = 0x80000000UL;
147 /* MSB is 1; assuring non-zero initial array */
153 gmx_rng_destroy(gmx_rng_t rng)
161 gmx_rng_get_state(gmx_rng_t rng, unsigned int *mt,int *mti)
165 for(i=0; i<RNG_N; i++) {
173 gmx_rng_set_state(gmx_rng_t rng, unsigned int *mt,int mti)
177 for(i=0; i<RNG_N; i++) {
185 gmx_rng_make_seed(void)
193 fp=fopen("/dev/random","rb"); /* will return NULL if it is not present */
198 ret=fread(&data,sizeof(unsigned int),1,fp);
201 /* No random device available, use time-of-day and process id */
202 #ifdef GMX_NATIVE_WINDOWS
203 my_pid = (long)_getpid();
205 my_pid = (long)getpid();
207 data=(unsigned int)(((long)time(NULL)+my_pid) % (long)1000000);
213 /* The random number state contains RNG_N entries that are returned one by
214 * one as random numbers. When we run out of them, this routine is called to
215 * regenerate RNG_N new entries.
218 gmx_rng_update(gmx_rng_t rng)
220 unsigned int lastx,x1,x2,y,*mt;
222 const unsigned int mag01[2] = {0x0UL, RNG_MATRIX_A};
223 /* mag01[x] = x * MATRIX_A for x=0,1 */
225 /* update random numbers */
226 mt = rng->mt; /* pointer to array - avoid repeated dereferencing */
230 for (k = 0; k < RNG_N-RNG_M-3; k += 4)
233 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
234 mt[k] = mt[k+RNG_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
236 y = (x2 & RNG_UPPER_MASK) | (x1 & RNG_LOWER_MASK);
237 mt[k+1] = mt[k+RNG_M+1] ^ (y >> 1) ^ mag01[y & 0x1UL];
239 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
240 mt[k+2] = mt[k+RNG_M+2] ^ (y >> 1) ^ mag01[y & 0x1UL];
242 y = (x2 & RNG_UPPER_MASK) | (x1 & RNG_LOWER_MASK);
243 mt[k+3] = mt[k+RNG_M+3] ^ (y >> 1) ^ mag01[y & 0x1UL];
246 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
247 mt[k] = mt[k+RNG_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
250 y = (x2 & RNG_UPPER_MASK) | (x1 & RNG_LOWER_MASK);
251 mt[k] = mt[k+RNG_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
254 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
255 mt[k] = mt[k+RNG_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
257 for (; k < RNG_N-1; k += 4)
260 y = (x2 & RNG_UPPER_MASK) | (x1 & RNG_LOWER_MASK);
261 mt[k] = mt[k+(RNG_M-RNG_N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
263 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
264 mt[k+1] = mt[k+(RNG_M-RNG_N)+1] ^ (y >> 1) ^ mag01[y & 0x1UL];
266 y = (x2 & RNG_UPPER_MASK) | (x1 & RNG_LOWER_MASK);
267 mt[k+2] = mt[k+(RNG_M-RNG_N)+2] ^ (y >> 1) ^ mag01[y & 0x1UL];
269 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
270 mt[k+3] = mt[k+(RNG_M-RNG_N)+3] ^ (y >> 1) ^ mag01[y & 0x1UL];
272 y = (x2 & RNG_UPPER_MASK) | (mt[0] & RNG_LOWER_MASK);
273 mt[RNG_N-1] = mt[RNG_M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
280 gmx_rng_gaussian_real(gmx_rng_t rng)
286 return rng->gauss_saved;
289 x=2.0*gmx_rng_uniform_real(rng)-1.0;
290 y=2.0*gmx_rng_uniform_real(rng)-1.0;
292 } while(r>1.0 || r==0.0);
294 r=sqrt(-2.0*log(r)/r);
295 rng->gauss_saved=y*r; /* save second random number */
297 return x*r; /* return first random number */
304 /* Return a random unsigned integer, i.e. 0..4294967295
305 * Provided in header file for performace reasons.
306 * Unfortunately this function cannot be inlined, since
307 * it needs to refer the internal-linkage gmx_rng_update().
310 gmx_rng_uniform_uint32(gmx_rng_t rng)
316 y=rng->mt[rng->mti++];
319 y ^= (y << 7) & 0x9d2c5680UL;
320 y ^= (y << 15) & 0xefc60000UL;
330 /* Return a uniform floating point number on the interval 0<=x<1 */
332 gmx_rng_uniform_real(gmx_rng_t rng)
334 if(sizeof(real)==sizeof(double))
335 return ((double)gmx_rng_uniform_uint32(rng))*(1.0/4294967296.0);
337 return ((float)gmx_rng_uniform_uint32(rng))*(1.0/4294967423.0);
338 /* divided by the smallest number that will generate a
339 * single precision real number on 0<=x<1.
340 * This needs to be slightly larger than MAX_UNIT since
341 * we are limited to an accuracy of 1e-7.
348 gmx_rng_gaussian_table(gmx_rng_t rng)
352 i = gmx_rng_uniform_uint32(rng);
354 /* The Gaussian table is a static constant in this file */
355 return gaussian_table[i >> GAUSS_SHIFT];