1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2008, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * Groningen Machine for Chemical Simulation
38 #include "gmx_header_config.h"
40 #include <gmx_random.h>
49 #ifdef GMX_NATIVE_WINDOWS
54 #include "gmx_random_gausstable.h"
58 #define RNG_MATRIX_A 0x9908b0dfUL /* constant vector a */
59 #define RNG_UPPER_MASK 0x80000000UL /* most significant w-r bits */
60 #define RNG_LOWER_MASK 0x7fffffffUL /* least significant r bits */
62 /* Note that if you change the size of the Gaussian table you will also
63 * have to generate new initialization data for the table in
64 * gmx_random_gausstable.h
66 * A routine print_gaussian_table() is in contrib/random.c
67 * for convenience - use it if you need a different size of the table.
69 #define GAUSS_TABLE 14 /* the size of the gauss table is 2^GAUSS_TABLE */
70 #define GAUSS_SHIFT (32 - GAUSS_TABLE)
74 unsigned int mt[RNG_N];
90 gmx_rng_init(unsigned int seed)
94 if((rng=(struct gmx_rng *)malloc(sizeof(struct gmx_rng)))==NULL)
97 rng->has_saved=0; /* no saved gaussian number yet */
99 rng->mt[0]= seed & 0xffffffffUL;
100 for (rng->mti=1; rng->mti<RNG_N; rng->mti++) {
102 (1812433253UL * (rng->mt[rng->mti-1] ^
103 (rng->mt[rng->mti-1] >> 30)) + rng->mti);
104 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
105 /* In the previous versions, MSBs of the seed affect */
106 /* only MSBs of the array mt[]. */
107 /* 2002/01/09 modified by Makoto Matsumoto */
108 rng->mt[rng->mti] &= 0xffffffffUL;
109 /* for >32 bit machines */
115 gmx_rng_init_array(unsigned int seed[], int seed_length)
120 if((rng=gmx_rng_init(19650218UL))==NULL)
124 k = (RNG_N>seed_length ? RNG_N : seed_length);
126 rng->mt[i] = (rng->mt[i] ^ ((rng->mt[i-1] ^
127 (rng->mt[i-1] >> 30)) * 1664525UL))
128 + seed[j] + j; /* non linear */
129 rng->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
131 if (i>=RNG_N) { rng->mt[0] = rng->mt[RNG_N-1]; i=1; }
132 if (j>=seed_length) j=0;
134 for (k=RNG_N-1; k; k--) {
135 rng->mt[i] = (rng->mt[i] ^ ((rng->mt[i-1] ^
136 (rng->mt[i-1] >> 30)) *
138 - i; /* non linear */
139 rng->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
141 if (i>=RNG_N) { rng->mt[0] = rng->mt[RNG_N-1]; i=1; }
144 rng->mt[0] = 0x80000000UL;
145 /* MSB is 1; assuring non-zero initial array */
151 gmx_rng_destroy(gmx_rng_t rng)
159 gmx_rng_get_state(gmx_rng_t rng, unsigned int *mt,int *mti)
163 for(i=0; i<RNG_N; i++) {
171 gmx_rng_set_state(gmx_rng_t rng, unsigned int *mt,int mti)
175 for(i=0; i<RNG_N; i++) {
183 gmx_rng_make_seed(void)
191 fp=fopen("/dev/random","rb"); /* will return NULL if it is not present */
196 ret=fread(&data,sizeof(unsigned int),1,fp);
199 /* No random device available, use time-of-day and process id */
200 #ifdef GMX_NATIVE_WINDOWS
201 my_pid = (long)_getpid();
203 my_pid = (long)getpid();
205 data=(unsigned int)(((long)time(NULL)+my_pid) % (long)1000000);
211 /* The random number state contains RNG_N entries that are returned one by
212 * one as random numbers. When we run out of them, this routine is called to
213 * regenerate RNG_N new entries.
216 gmx_rng_update(gmx_rng_t rng)
218 unsigned int lastx,x1,x2,y,*mt;
220 const unsigned int mag01[2] = {0x0UL, RNG_MATRIX_A};
221 /* mag01[x] = x * MATRIX_A for x=0,1 */
223 /* update random numbers */
224 mt = rng->mt; /* pointer to array - avoid repeated dereferencing */
228 for (k = 0; k < RNG_N-RNG_M-3; k += 4)
231 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
232 mt[k] = mt[k+RNG_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
234 y = (x2 & RNG_UPPER_MASK) | (x1 & RNG_LOWER_MASK);
235 mt[k+1] = mt[k+RNG_M+1] ^ (y >> 1) ^ mag01[y & 0x1UL];
237 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
238 mt[k+2] = mt[k+RNG_M+2] ^ (y >> 1) ^ mag01[y & 0x1UL];
240 y = (x2 & RNG_UPPER_MASK) | (x1 & RNG_LOWER_MASK);
241 mt[k+3] = mt[k+RNG_M+3] ^ (y >> 1) ^ mag01[y & 0x1UL];
244 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
245 mt[k] = mt[k+RNG_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
248 y = (x2 & RNG_UPPER_MASK) | (x1 & RNG_LOWER_MASK);
249 mt[k] = mt[k+RNG_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
252 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
253 mt[k] = mt[k+RNG_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
255 for (; k < RNG_N-1; k += 4)
258 y = (x2 & RNG_UPPER_MASK) | (x1 & RNG_LOWER_MASK);
259 mt[k] = mt[k+(RNG_M-RNG_N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
261 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
262 mt[k+1] = mt[k+(RNG_M-RNG_N)+1] ^ (y >> 1) ^ mag01[y & 0x1UL];
264 y = (x2 & RNG_UPPER_MASK) | (x1 & RNG_LOWER_MASK);
265 mt[k+2] = mt[k+(RNG_M-RNG_N)+2] ^ (y >> 1) ^ mag01[y & 0x1UL];
267 y = (x1 & RNG_UPPER_MASK) | (x2 & RNG_LOWER_MASK);
268 mt[k+3] = mt[k+(RNG_M-RNG_N)+3] ^ (y >> 1) ^ mag01[y & 0x1UL];
270 y = (x2 & RNG_UPPER_MASK) | (mt[0] & RNG_LOWER_MASK);
271 mt[RNG_N-1] = mt[RNG_M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
278 gmx_rng_gaussian_real(gmx_rng_t rng)
284 return rng->gauss_saved;
287 x=2.0*gmx_rng_uniform_real(rng)-1.0;
288 y=2.0*gmx_rng_uniform_real(rng)-1.0;
290 } while(r>1.0 || r==0.0);
292 r=sqrt(-2.0*log(r)/r);
293 rng->gauss_saved=y*r; /* save second random number */
295 return x*r; /* return first random number */
302 /* Return a random unsigned integer, i.e. 0..4294967295
303 * Provided in header file for performace reasons.
304 * Unfortunately this function cannot be inlined, since
305 * it needs to refer the internal-linkage gmx_rng_update().
308 gmx_rng_uniform_uint32(gmx_rng_t rng)
314 y=rng->mt[rng->mti++];
317 y ^= (y << 7) & 0x9d2c5680UL;
318 y ^= (y << 15) & 0xefc60000UL;
328 /* Return a uniform floating point number on the interval 0<=x<1 */
330 gmx_rng_uniform_real(gmx_rng_t rng)
332 if(sizeof(real)==sizeof(double))
333 return ((double)gmx_rng_uniform_uint32(rng))*(1.0/4294967296.0);
335 return ((float)gmx_rng_uniform_uint32(rng))*(1.0/4294967423.0);
336 /* divided by the smallest number that will generate a
337 * single precision real number on 0<=x<1.
338 * This needs to be slightly larger than MAX_UNIT since
339 * we are limited to an accuracy of 1e-7.
346 gmx_rng_gaussian_table(gmx_rng_t rng)
350 i = gmx_rng_uniform_uint32(rng);
352 /* The Gaussian table is a static constant in this file */
353 return gaussian_table[i >> GAUSS_SHIFT];