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, 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.
41 #include "gmx_header_config.h"
43 #include <gmx_random.h>
52 #ifdef GMX_NATIVE_WINDOWS
57 #include "gmx_random_gausstable.h"
61 #define RNG_MATRIX_A 0x9908b0dfUL /* constant vector a */
62 #define RNG_UPPER_MASK 0x80000000UL /* most significant w-r bits */
63 #define RNG_LOWER_MASK 0x7fffffffUL /* least significant r bits */
65 /* Note that if you change the size of the Gaussian table you will also
66 * have to generate new initialization data for the table in
67 * gmx_random_gausstable.h
69 * A routine print_gaussian_table() is in contrib/random.c
70 * for convenience - use it if you need a different size of the table.
72 #define GAUSS_TABLE 14 /* the size of the gauss table is 2^GAUSS_TABLE */
73 #define GAUSS_SHIFT (32 - GAUSS_TABLE)
77 unsigned int mt[RNG_N];
93 gmx_rng_init(unsigned int seed)
97 if((rng=(struct gmx_rng *)malloc(sizeof(struct gmx_rng)))==NULL)
100 rng->has_saved=0; /* no saved gaussian number yet */
102 rng->mt[0]= seed & 0xffffffffUL;
103 for (rng->mti=1; rng->mti<RNG_N; rng->mti++) {
105 (1812433253UL * (rng->mt[rng->mti-1] ^
106 (rng->mt[rng->mti-1] >> 30)) + rng->mti);
107 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
108 /* In the previous versions, MSBs of the seed affect */
109 /* only MSBs of the array mt[]. */
110 /* 2002/01/09 modified by Makoto Matsumoto */
111 rng->mt[rng->mti] &= 0xffffffffUL;
112 /* for >32 bit machines */
118 gmx_rng_init_array(unsigned int seed[], int seed_length)
123 if((rng=gmx_rng_init(19650218UL))==NULL)
127 k = (RNG_N>seed_length ? RNG_N : seed_length);
129 rng->mt[i] = (rng->mt[i] ^ ((rng->mt[i-1] ^
130 (rng->mt[i-1] >> 30)) * 1664525UL))
131 + seed[j] + j; /* non linear */
132 rng->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
134 if (i>=RNG_N) { rng->mt[0] = rng->mt[RNG_N-1]; i=1; }
135 if (j>=seed_length) j=0;
137 for (k=RNG_N-1; k; k--) {
138 rng->mt[i] = (rng->mt[i] ^ ((rng->mt[i-1] ^
139 (rng->mt[i-1] >> 30)) *
141 - i; /* non linear */
142 rng->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
144 if (i>=RNG_N) { rng->mt[0] = rng->mt[RNG_N-1]; i=1; }
147 rng->mt[0] = 0x80000000UL;
148 /* MSB is 1; assuring non-zero initial array */
154 gmx_rng_destroy(gmx_rng_t rng)
162 gmx_rng_get_state(gmx_rng_t rng, unsigned int *mt,int *mti)
166 for(i=0; i<RNG_N; i++) {
174 gmx_rng_set_state(gmx_rng_t rng, unsigned int *mt,int mti)
178 for(i=0; i<RNG_N; i++) {
186 gmx_rng_make_seed(void)
194 fp=fopen("/dev/random","rb"); /* will return NULL if it is not present */
199 ret=fread(&data,sizeof(unsigned int),1,fp);
202 /* No random device available, use time-of-day and process id */
203 #ifdef GMX_NATIVE_WINDOWS
204 my_pid = (long)_getpid();
206 my_pid = (long)getpid();
208 data=(unsigned int)(((long)time(NULL)+my_pid) % (long)1000000);
214 /* The random number state contains RNG_N entries that are returned one by
215 * one as random numbers. When we run out of them, this routine is called to
216 * regenerate RNG_N new entries.
219 gmx_rng_update(gmx_rng_t rng)
221 unsigned int lastx,x1,x2,y,*mt;
223 const unsigned int mag01[2] = {0x0UL, RNG_MATRIX_A};
224 /* mag01[x] = x * MATRIX_A for x=0,1 */
226 /* update random numbers */
227 mt = rng->mt; /* pointer to array - avoid repeated dereferencing */
231 for (k = 0; k < RNG_N-RNG_M-3; k += 4)
234 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
235 mt[k] = mt[k+RNG_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
237 y = (x2 & RNG_UPPER_MASK) | (x1 & RNG_LOWER_MASK);
238 mt[k+1] = mt[k+RNG_M+1] ^ (y >> 1) ^ mag01[y & 0x1UL];
240 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
241 mt[k+2] = mt[k+RNG_M+2] ^ (y >> 1) ^ mag01[y & 0x1UL];
243 y = (x2 & RNG_UPPER_MASK) | (x1 & RNG_LOWER_MASK);
244 mt[k+3] = mt[k+RNG_M+3] ^ (y >> 1) ^ mag01[y & 0x1UL];
247 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
248 mt[k] = mt[k+RNG_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
251 y = (x2 & RNG_UPPER_MASK) | (x1 & RNG_LOWER_MASK);
252 mt[k] = mt[k+RNG_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
255 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
256 mt[k] = mt[k+RNG_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
258 for (; k < RNG_N-1; k += 4)
261 y = (x2 & RNG_UPPER_MASK) | (x1 & RNG_LOWER_MASK);
262 mt[k] = mt[k+(RNG_M-RNG_N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
264 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
265 mt[k+1] = mt[k+(RNG_M-RNG_N)+1] ^ (y >> 1) ^ mag01[y & 0x1UL];
267 y = (x2 & RNG_UPPER_MASK) | (x1 & RNG_LOWER_MASK);
268 mt[k+2] = mt[k+(RNG_M-RNG_N)+2] ^ (y >> 1) ^ mag01[y & 0x1UL];
270 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
271 mt[k+3] = mt[k+(RNG_M-RNG_N)+3] ^ (y >> 1) ^ mag01[y & 0x1UL];
273 y = (x2 & RNG_UPPER_MASK) | (mt[0] & RNG_LOWER_MASK);
274 mt[RNG_N-1] = mt[RNG_M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
281 gmx_rng_gaussian_real(gmx_rng_t rng)
287 return rng->gauss_saved;
290 x=2.0*gmx_rng_uniform_real(rng)-1.0;
291 y=2.0*gmx_rng_uniform_real(rng)-1.0;
293 } while(r>1.0 || r==0.0);
295 r=sqrt(-2.0*log(r)/r);
296 rng->gauss_saved=y*r; /* save second random number */
298 return x*r; /* return first random number */
305 /* Return a random unsigned integer, i.e. 0..4294967295
306 * Provided in header file for performace reasons.
307 * Unfortunately this function cannot be inlined, since
308 * it needs to refer the internal-linkage gmx_rng_update().
311 gmx_rng_uniform_uint32(gmx_rng_t rng)
317 y=rng->mt[rng->mti++];
320 y ^= (y << 7) & 0x9d2c5680UL;
321 y ^= (y << 15) & 0xefc60000UL;
331 /* Return a uniform floating point number on the interval 0<=x<1 */
333 gmx_rng_uniform_real(gmx_rng_t rng)
335 if(sizeof(real)==sizeof(double))
336 return ((double)gmx_rng_uniform_uint32(rng))*(1.0/4294967296.0);
338 return ((float)gmx_rng_uniform_uint32(rng))*(1.0/4294967423.0);
339 /* divided by the smallest number that will generate a
340 * single precision real number on 0<=x<1.
341 * This needs to be slightly larger than MAX_UNIT since
342 * we are limited to an accuracy of 1e-7.
349 gmx_rng_gaussian_table(gmx_rng_t rng)
353 i = gmx_rng_uniform_uint32(rng);
355 /* The Gaussian table is a static constant in this file */
356 return gaussian_table[i >> GAUSS_SHIFT];