8bfbf8b7660d859815831fa629b192db9bfc17b7
[alexxy/gromacs.git] / src / gromacs / legacyheaders / gmx_random.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2008, The GROMACS development team.
6  * Copyright (c) 2010, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37
38 #ifndef _GMX_RANDOM_H_
39 #define _GMX_RANDOM_H_
40
41 #include <stdio.h>
42 #include "types/simple.h"
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 /*! \brief Abstract datatype for a random number generator
49  *
50  * This is a handle to the full state of a random number generator.
51  * You can not access anything inside the gmx_rng structure outside this
52  * file.
53  */
54 typedef struct gmx_rng *
55     gmx_rng_t;
56
57
58 /*! \brief Returns the size of the RNG integer data structure
59  *
60  * Returns the size of the RNG integer data structure.
61  * \threadsafe Yes.
62  */
63 int
64 gmx_rng_n(void);
65
66
67 /*! \brief Create a new RNG, seeded from a single integer.
68  *
69  * If you dont want to pick a seed, just call it as
70  * rng=gmx_rng_init(gmx_rng_make_seed()) to seed it from
71  * the system time or a random device.
72  *
73  * \param seed Random seed, unsigned 32-bit integer.
74  *
75  * \return Reference to a random number generator, or NULL if there was an
76  *         error.
77  *
78  * \threadsafe Yes.
79  */
80 gmx_rng_t
81 gmx_rng_init(unsigned int seed);
82
83
84 /*! \brief Generate a 'random' RNG seed.
85  *
86  * This routine tries to get a seed from /dev/random if present,
87  * and if not it uses time-of-day and process id to generate one.
88  *
89  * \return 32-bit unsigned integer random seed.
90  *
91  * Tip: If you use this in your code, it is a good idea to write the
92  * returned random seed to a logfile, so you can recreate the exact sequence
93  * of random number if you need to reproduce your run later for one reason
94  * or another.
95  *
96  * \threadsafe Yes.
97  */
98 unsigned int
99 gmx_rng_make_seed(void);
100
101
102 /*! \brief Initialize a RNG with 624 integers (>32 bits of entropy).
103  *
104  *  The Mersenne twister RNG used in Gromacs has an extremely long period,
105  *  but when you only initialize it with a 32-bit integer there are only
106  *  2^32 different possible sequences of number - much less than the generator
107  *  is capable of.
108  *
109  *  If you really need the full entropy, this routine makes it possible to
110  *  initialize the RNG with up to 624 32-bit integers, which will give you
111  *  up to 2^19968 bits of entropy.
112  *
113  *  \param seed Array of unsigned integers to form a seed
114  *  \param seed_length Number of integers in the array, up to 624 are used.
115  *
116  * \return Reference to a random number generator, or NULL if there was an
117  *         error.
118  *
119  * \threadsafe Yes.
120  */
121 gmx_rng_t
122 gmx_rng_init_array(unsigned int    seed[],
123                    int             seed_length);
124
125
126 /*! \brief Release resources of a RNG
127  *
128  *  This routine destroys a random number generator and releases all
129  *  resources allocated by it.
130  *
131  *  \param rng Handle to random number generator previously returned by
132  *                     gmx_rng_init() or gmx_rng_init_array().
133  *
134  * \threadsafe Function itself is threadsafe, but you should only destroy a
135  *             certain RNG once (i.e. from one thread).
136  */
137 void
138 gmx_rng_destroy(gmx_rng_t rng);
139
140
141 /*! \brief Get the state of a RNG
142  *
143  *  This routine stores the random state in mt and mti, mt should have
144  *  a size of at least 624, mt of 1.
145  *
146  *  \param rng Handle to random number generator previously returned by
147  *                     gmx_rng_init() or gmx_rng_init_array().
148  */
149 void
150 gmx_rng_get_state(gmx_rng_t rng, unsigned int *mt, int *mti);
151
152
153 /*! \brief Set the state of a RNG
154  *
155  *  This routine sets the random state from mt and mti, mt should have
156  *  a size of at least 624.
157  *
158  *  \param rng Handle to random number generator previously returned by
159  *                     gmx_rng_init() or gmx_rng_init_array().
160  */
161 void
162 gmx_rng_set_state(gmx_rng_t rng, unsigned int *mt, int mti);
163
164
165 /*! \brief Random 32-bit integer from a uniform distribution
166  *
167  *  This routine returns a random integer from the random number generator
168  *  provided, and updates the state of that RNG.
169  *
170  *  \param rng Handle to random number generator previously returned by
171  *                     gmx_rng_init() or gmx_rng_init_array().
172  *
173  *  \return 32-bit unsigned integer from a uniform distribution.
174  *
175  *  \threadsafe Function yes, input data no. You should not call this function
176  *              from two different threads using the same RNG handle at the
177  *              same time. For performance reasons we cannot lock the handle
178  *              with a mutex every time we need a random number - that would
179  *              slow the routine down a factor 2-5. There are two simple
180  *              solutions: either use a mutex and lock it before calling
181  *              the function, or use a separate RNG handle for each thread.
182  */
183 unsigned int
184 gmx_rng_uniform_uint32(gmx_rng_t rng);
185
186
187 /*! \brief Random gmx_real_t 0<=x<1 from a uniform distribution
188  *
189  *  This routine returns a random floating-point number from the
190  *  random number generator provided, and updates the state of that RNG.
191  *
192  *  \param rng Handle to random number generator previously returned by
193  *                     gmx_rng_init() or gmx_rng_init_array().
194  *
195  *  \return floating-point number 0<=x<1 from a uniform distribution.
196  *
197  *  \threadsafe Function yes, input data no. You should not call this function
198  *              from two different threads using the same RNG handle at the
199  *              same time. For performance reasons we cannot lock the handle
200  *              with a mutex every time we need a random number - that would
201  *              slow the routine down a factor 2-5. There are two simple
202  *              solutions: either use a mutex and lock it before calling
203  *              the function, or use a separate RNG handle for each thread.
204  */
205 real
206 gmx_rng_uniform_real(gmx_rng_t rng);
207
208
209 /*! \brief Random gmx_real_t from a gaussian distribution
210  *
211  *  This routine returns a random floating-point number from the
212  *  random number generator provided, and updates the state of that RNG.
213  *
214  *  The Box-Muller algorithm is used to provide gaussian random numbers. This
215  *  is not the fastest known algorithm for gaussian numbers, but in contrast
216  *  to the alternatives it is very well studied and you can trust the returned
217  *  random numbers to have good properties and no correlations.
218  *
219  *  \param rng Handle to random number generator previously returned by
220  *                        gmx_rng_init() or gmx_rng_init_array().
221  *
222  *  \return Gaussian random floating-point number with average 0.0 and
223  *          standard deviation 1.0. You can get any average/mean you want
224  *          by first multiplying with the desired average and then adding
225  *          the average you want.
226  *
227  *  \threadsafe Function yes, input data no. You should not call this function
228  *              from two different threads using the same RNG handle at the
229  *              same time. For performance reasons we cannot lock the handle
230  *              with a mutex every time we need a random number - that would
231  *              slow the routine down a factor 2-5. There are two simple
232  *              solutions: either use a mutex and lock it before calling
233  *              the function, or use a separate RNG handle for each thread.
234  *
235  *  It works perfectly to mix calls to get uniform and gaussian random numbers
236  *  from the same generator, but since it will affect the sequence of returned
237  *  numbers it is probably better to use separate random number generator
238  *  structures.
239  */
240 real
241 gmx_rng_gaussian_real(gmx_rng_t rng);
242
243
244
245 /* Return a new gaussian random number with expectation value
246  * 0.0 and standard deviation 1.0. This routine uses a table
247  * lookup for maximum speed.
248  *
249  * WARNING: The lookup table is 16k by default, which means
250  *          the granularity of the random numbers is coarser
251  *          than what you get from gmx_rng_gauss_real().
252  *          In most cases this is no problem whatsoever,
253  *          and it is particularly true for BD/SD integration.
254  *          Note that you will NEVER get any really extreme
255  *          numbers: the maximum absolute value returned is
256  *          4.0255485.
257  *
258  * threadsafe: yes
259  */
260 real
261 gmx_rng_gaussian_table(gmx_rng_t rng);
262
263 #ifdef __cplusplus
264 }
265 #endif
266
267 #endif /* _GMX_RANDOM_H_ */