Run regressiontests from build
[gromacs.git] / include / gmx_random.h
blob5df5ed05e9c106288977cb5cc4d7ac1836387577
1 /*
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.
39 #ifndef _GMX_RANDOM_H_
40 #define _GMX_RANDOM_H_
41 #include "visibility.h"
42 #include <stdio.h>
43 #include "types/simple.h"
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
49 /*! \brief Abstract datatype for a random number generator
51 * This is a handle to the full state of a random number generator.
52 * You can not access anything inside the gmx_rng structure outside this
53 * file.
55 typedef struct gmx_rng *
56 gmx_rng_t;
59 /*! \brief Returns the size of the RNG integer data structure
61 * Returns the size of the RNG integer data structure.
62 * \threadsafe Yes.
64 GMX_LIBGMX_EXPORT
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_LIBGMX_EXPORT
83 gmx_rng_t
84 gmx_rng_init(unsigned int seed);
87 /*! \brief Generate a 'random' RNG seed.
89 * This routine tries to get a seed from /dev/random if present,
90 * and if not it uses time-of-day and process id to generate one.
92 * \return 32-bit unsigned integer random seed.
94 * Tip: If you use this in your code, it is a good idea to write the
95 * returned random seed to a logfile, so you can recreate the exact sequence
96 * of random number if you need to reproduce your run later for one reason
97 * or another.
99 * \threadsafe Yes.
101 GMX_LIBGMX_EXPORT
102 unsigned int
103 gmx_rng_make_seed(void);
106 /*! \brief Initialize a RNG with 624 integers (>32 bits of entropy).
108 * The Mersenne twister RNG used in Gromacs has an extremely long period,
109 * but when you only initialize it with a 32-bit integer there are only
110 * 2^32 different possible sequences of number - much less than the generator
111 * is capable of.
113 * If you really need the full entropy, this routine makes it possible to
114 * initialize the RNG with up to 624 32-bit integers, which will give you
115 * up to 2^19968 bits of entropy.
117 * \param seed Array of unsigned integers to form a seed
118 * \param seed_length Number of integers in the array, up to 624 are used.
120 * \return Reference to a random number generator, or NULL if there was an
121 * error.
123 * \threadsafe Yes.
125 gmx_rng_t
126 gmx_rng_init_array(unsigned int seed[],
127 int seed_length);
130 /*! \brief Release resources of a RNG
132 * This routine destroys a random number generator and releases all
133 * resources allocated by it.
135 * \param rng Handle to random number generator previously returned by
136 * gmx_rng_init() or gmx_rng_init_array().
138 * \threadsafe Function itself is threadsafe, but you should only destroy a
139 * certain RNG once (i.e. from one thread).
141 GMX_LIBGMX_EXPORT
142 void
143 gmx_rng_destroy(gmx_rng_t rng);
146 /*! \brief Get the state of a RNG
148 * This routine stores the random state in mt and mti, mt should have
149 * a size of at least 624, mt of 1.
151 * \param rng Handle to random number generator previously returned by
152 * gmx_rng_init() or gmx_rng_init_array().
154 GMX_LIBGMX_EXPORT
155 void
156 gmx_rng_get_state(gmx_rng_t rng, unsigned int *mt,int *mti);
159 /*! \brief Set the state of a RNG
161 * This routine sets the random state from mt and mti, mt should have
162 * a size of at least 624.
164 * \param rng Handle to random number generator previously returned by
165 * gmx_rng_init() or gmx_rng_init_array().
167 GMX_LIBGMX_EXPORT
168 void
169 gmx_rng_set_state(gmx_rng_t rng, unsigned int *mt,int mti);
172 /*! \brief Random 32-bit integer from a uniform distribution
174 * This routine returns a random integer from the random number generator
175 * provided, and updates the state of that RNG.
177 * \param rng Handle to random number generator previously returned by
178 * gmx_rng_init() or gmx_rng_init_array().
180 * \return 32-bit unsigned integer from a uniform distribution.
182 * \threadsafe Function yes, input data no. You should not call this function
183 * from two different threads using the same RNG handle at the
184 * same time. For performance reasons we cannot lock the handle
185 * with a mutex every time we need a random number - that would
186 * slow the routine down a factor 2-5. There are two simple
187 * solutions: either use a mutex and lock it before calling
188 * the function, or use a separate RNG handle for each thread.
190 GMX_LIBGMX_EXPORT
191 unsigned int
192 gmx_rng_uniform_uint32(gmx_rng_t rng);
195 /*! \brief Random gmx_real_t 0<=x<1 from a uniform distribution
197 * This routine returns a random floating-point number from the
198 * random number generator provided, and updates the state of that RNG.
200 * \param rng Handle to random number generator previously returned by
201 * gmx_rng_init() or gmx_rng_init_array().
203 * \return floating-point number 0<=x<1 from a uniform distribution.
205 * \threadsafe Function yes, input data no. You should not call this function
206 * from two different threads using the same RNG handle at the
207 * same time. For performance reasons we cannot lock the handle
208 * with a mutex every time we need a random number - that would
209 * slow the routine down a factor 2-5. There are two simple
210 * solutions: either use a mutex and lock it before calling
211 * the function, or use a separate RNG handle for each thread.
213 GMX_LIBGMX_EXPORT
214 real
215 gmx_rng_uniform_real(gmx_rng_t rng);
218 /*! \brief Random gmx_real_t from a gaussian distribution
220 * This routine returns a random floating-point number from the
221 * random number generator provided, and updates the state of that RNG.
223 * The Box-Muller algorithm is used to provide gaussian random numbers. This
224 * is not the fastest known algorithm for gaussian numbers, but in contrast
225 * to the alternatives it is very well studied and you can trust the returned
226 * random numbers to have good properties and no correlations.
228 * \param rng Handle to random number generator previously returned by
229 * gmx_rng_init() or gmx_rng_init_array().
231 * \return Gaussian random floating-point number with average 0.0 and
232 * standard deviation 1.0. You can get any average/mean you want
233 * by first multiplying with the desired average and then adding
234 * the average you want.
236 * \threadsafe Function yes, input data no. You should not call this function
237 * from two different threads using the same RNG handle at the
238 * same time. For performance reasons we cannot lock the handle
239 * with a mutex every time we need a random number - that would
240 * slow the routine down a factor 2-5. There are two simple
241 * solutions: either use a mutex and lock it before calling
242 * the function, or use a separate RNG handle for each thread.
244 * It works perfectly to mix calls to get uniform and gaussian random numbers
245 * from the same generator, but since it will affect the sequence of returned
246 * numbers it is probably better to use separate random number generator
247 * structures.
249 GMX_LIBGMX_EXPORT
250 real
251 gmx_rng_gaussian_real(gmx_rng_t rng);
255 /* Return a new gaussian random number with expectation value
256 * 0.0 and standard deviation 1.0. This routine uses a table
257 * lookup for maximum speed.
259 * WARNING: The lookup table is 16k by default, which means
260 * the granularity of the random numbers is coarser
261 * than what you get from gmx_rng_gauss_real().
262 * In most cases this is no problem whatsoever,
263 * and it is particularly true for BD/SD integration.
264 * Note that you will NEVER get any really extreme
265 * numbers: the maximum absolute value returned is
266 * 4.0255485.
268 * threadsafe: yes
270 GMX_LIBGMX_EXPORT
271 real
272 gmx_rng_gaussian_table(gmx_rng_t rng);
274 #ifdef __cplusplus
276 #endif
278 #endif /* _GMX_RANDOM_H_ */