Bumped version to 4.5-beta3
[gromacs.git] / include / gmx_random.h
blob206bd8cbac70874272162ca56e033107379eb8bb
1 /*
2 *
3 * This source code is part of
4 *
5 * G R O M A C S
6 *
7 * GROningen MAchine for Chemical Simulations
8 *
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2008, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
31 * And Hey:
32 * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
35 #ifndef _GMX_RANDOM_H_
36 #define _GMX_RANDOM_H_
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
43 #include <stdio.h>
44 #include "types/simple.h"
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
50 /*! \brief Abstract datatype for a random number generator
52 * This is a handle to the full state of a random number generator.
53 * You can not access anything inside the gmx_rng structure outside this
54 * file.
56 typedef struct gmx_rng *
57 gmx_rng_t;
60 /*! \brief Returns the size of the RNG integer data structure
62 * Returns the size of the RNG integer data structure.
63 * \threadsafe Yes.
65 int
66 gmx_rng_n(void);
69 /*! \brief Create a new RNG, seeded from a single integer.
71 * If you dont want to pick a seed, just call it as
72 * rng=gmx_rng_init(gmx_rng_make_seed()) to seed it from
73 * the system time or a random device.
75 * \param seed Random seed, unsigned 32-bit integer.
77 * \return Reference to a random number generator, or NULL if there was an
78 * error.
80 * \threadsafe Yes.
82 gmx_rng_t
83 gmx_rng_init(unsigned int seed);
86 /*! \brief Generate a 'random' RNG seed.
88 * This routine tries to get a seed from /dev/random if present,
89 * and if not it uses time-of-day and process id to generate one.
91 * \return 32-bit unsigned integer random seed.
93 * Tip: If you use this in your code, it is a good idea to write the
94 * returned random seed to a logfile, so you can recreate the exact sequence
95 * of random number if you need to reproduce your run later for one reason
96 * or another.
98 * \threadsafe Yes.
100 unsigned int
101 gmx_rng_make_seed(void);
104 /*! \brief Initialize a RNG with 624 integers (>32 bits of entropy).
106 * The Mersenne twister RNG used in Gromacs has an extremely long period,
107 * but when you only initialize it with a 32-bit integer there are only
108 * 2^32 different possible sequences of number - much less than the generator
109 * is capable of.
111 * If you really need the full entropy, this routine makes it possible to
112 * initialize the RNG with up to 624 32-bit integers, which will give you
113 * up to 2^19968 bits of entropy.
115 * \param seed Array of unsigned integers to form a seed
116 * \param seed_length Number of integers in the array, up to 624 are used.
118 * \return Reference to a random number generator, or NULL if there was an
119 * error.
121 * \threadsafe Yes.
123 gmx_rng_t
124 gmx_rng_init_array(unsigned int seed[],
125 int seed_length);
128 /*! \brief Release resources of a RNG
130 * This routine destroys a random number generator and releases all
131 * resources allocated by it.
133 * \param rng Handle to random number generator previously returned by
134 * gmx_rng_init() or gmx_rng_init_array().
136 * \threadsafe Function itself is threadsafe, but you should only destroy a
137 * certain RNG once (i.e. from one thread).
139 void
140 gmx_rng_destroy(gmx_rng_t rng);
143 /*! \brief Get the state of a RNG
145 * This routine stores the random state in mt and mti, mt should have
146 * a size of at least 624, mt of 1.
148 * \param rng Handle to random number generator previously returned by
149 * gmx_rng_init() or gmx_rng_init_array().
151 void
152 gmx_rng_get_state(gmx_rng_t rng, unsigned int *mt,int *mti);
155 /*! \brief Set the state of a RNG
157 * This routine sets the random state from mt and mti, mt should have
158 * a size of at least 624.
160 * \param rng Handle to random number generator previously returned by
161 * gmx_rng_init() or gmx_rng_init_array().
163 void
164 gmx_rng_set_state(gmx_rng_t rng, unsigned int *mt,int mti);
167 /*! \brief Random 32-bit integer from a uniform distribution
169 * This routine returns a random integer from the random number generator
170 * provided, and updates the state of that RNG.
172 * \param rng Handle to random number generator previously returned by
173 * gmx_rng_init() or gmx_rng_init_array().
175 * \return 32-bit unsigned integer from a uniform distribution.
177 * \threadsafe Function yes, input data no. You should not call this function
178 * from two different threads using the same RNG handle at the
179 * same time. For performance reasons we cannot lock the handle
180 * with a mutex every time we need a random number - that would
181 * slow the routine down a factor 2-5. There are two simple
182 * solutions: either use a mutex and lock it before calling
183 * the function, or use a separate RNG handle for each thread.
185 unsigned int
186 gmx_rng_uniform_uint32(gmx_rng_t rng);
189 /*! \brief Random gmx_real_t 0<=x<1 from a uniform distribution
191 * This routine returns a random floating-point number from the
192 * random number generator provided, and updates the state of that RNG.
194 * \param rng Handle to random number generator previously returned by
195 * gmx_rng_init() or gmx_rng_init_array().
197 * \return floating-point number 0<=x<1 from a uniform distribution.
199 * \threadsafe Function yes, input data no. You should not call this function
200 * from two different threads using the same RNG handle at the
201 * same time. For performance reasons we cannot lock the handle
202 * with a mutex every time we need a random number - that would
203 * slow the routine down a factor 2-5. There are two simple
204 * solutions: either use a mutex and lock it before calling
205 * the function, or use a separate RNG handle for each thread.
207 real
208 gmx_rng_uniform_real(gmx_rng_t rng);
211 /*! \brief Random gmx_real_t from a gaussian distribution
213 * This routine returns a random floating-point number from the
214 * random number generator provided, and updates the state of that RNG.
216 * The Box-Muller algorithm is used to provide gaussian random numbers. This
217 * is not the fastest known algorithm for gaussian numbers, but in contrast
218 * to the alternatives it is very well studied and you can trust the returned
219 * random numbers to have good properties and no correlations.
221 * \param rng Handle to random number generator previously returned by
222 * gmx_rng_init() or gmx_rng_init_array().
224 * \return Gaussian random floating-point number with average 0.0 and
225 * standard deviation 1.0. You can get any average/mean you want
226 * by first multiplying with the desired average and then adding
227 * the average you want.
229 * \threadsafe Function yes, input data no. You should not call this function
230 * from two different threads using the same RNG handle at the
231 * same time. For performance reasons we cannot lock the handle
232 * with a mutex every time we need a random number - that would
233 * slow the routine down a factor 2-5. There are two simple
234 * solutions: either use a mutex and lock it before calling
235 * the function, or use a separate RNG handle for each thread.
237 * It works perfectly to mix calls to get uniform and gaussian random numbers
238 * from the same generator, but since it will affect the sequence of returned
239 * numbers it is probably better to use separate random number generator
240 * structures.
242 real
243 gmx_rng_gaussian_real(gmx_rng_t rng);
247 /* Return a new gaussian random number with expectation value
248 * 0.0 and standard deviation 1.0. This routine uses a table
249 * lookup for maximum speed.
251 * WARNING: The lookup table is 16k by default, which means
252 * the granularity of the random numbers is coarser
253 * than what you get from gmx_rng_gauss_real().
254 * In most cases this is no problem whatsoever,
255 * and it is particularly true for BD/SD integration.
256 * Note that you will NEVER get any really extreme
257 * numbers: the maximum absolute value returned is
258 * 4.0255485.
260 * threadsafe: yes
262 real
263 gmx_rng_gaussian_table(gmx_rng_t rng);
265 #ifdef __cplusplus
267 #endif
269 #endif /* _GMX_RANDOM_H_ */