-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrandom.h
executable file
·175 lines (155 loc) · 5.58 KB
/
random.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* *
* The C Protein Folding Library.
* Copyright (C) 2009 Andres Colubri.
* Contact: andres.colubri 'AT' gmail.com
*
* This library was written at the Institute of Biophysical Dynamics at the University of Chicago.
* Gordon Center for Integrated Science, W101. 929 East 57th Street, Chicago, IL 60637, USA.
* Homepage: http://ibd.uchicago.edu/
*
* Permission to use, copy, modify, and distribute this software and its
* documentation with or without modifications and for any purpose and
* without fee is hereby granted, provided that any copyright notices
* appear in all copies and that both those copyright notices and this
* permission notice appear in supporting documentation, and that the
* names of the contributors or copyright holders not be used in
* advertising or publicity pertaining to distribution of the software
* without specific prior permission.
*
* THE CONTRIBUTORS AND COPYRIGHT HOLDERS OF THIS SOFTWARE DISCLAIM ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE
* CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT
* OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
* OR PERFORMANCE OF THIS SOFTWARE.
*
*/
/**
* Implementation of a Mersenne Twister Random Number Generator.
* This random number generator is described in the article by
* M. Matsumoto & T. Nishimura, in:
* ACM Transactions on Modeling and Computer Simulation,
* vol. 8, no. 1, 1998, pp. 3-30.
* I has excellent statistical properties and it is very well suited for Monte Carlo simulations
* This code has been ported to C from the original C++ code by Agner Fog. This code can be
* found at http://www.agner.org.
* For more information on the Mersenne Twister, check prof. M. Matsumoto's website:
* http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
*
*/
#ifndef __RANDOM_H__
#define __RANDOM_H__
#define MAXINT 4294967296 // 65536*65536 = 2^32
#define INVMAXINT 0.00000000023283064365386962890625 // 2^-32
#define DOUBLESHIFT 0.000000000116415321826934814453125 // 2^-33
// Define integer types with known size: int32_t, uint32_t, int64_t, uint64_t.
// If this doesn't work then insert compiler-specific definitions here:
#if defined(__GNUC__)
// Compilers supporting C99 or C++0x have inttypes.h defining these integer types
#include <inttypes.h>
#define INT64_SUPPORTED // Remove this if the compiler doesn't support 64-bit integers
#elif defined(_WIN16) || defined(__MSDOS__) || defined(_MSDOS)
// 16 bit systems use long int for 32 bit integer
typedef signed long int int32_t;
typedef unsigned long int uint32_t;
#elif defined(_MSC_VER)
// Microsoft have their own definition
typedef signed __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef signed __int64 int64_t;
typedef unsigned __int64 uint64_t;
#define INT64_SUPPORTED // Remove this if the compiler doesn't support 64-bit integers
#else
// This works with most compilers
typedef signed int int32_t;
typedef unsigned int uint32_t;
typedef long long int64_t;
typedef unsigned long long uint64_t;
#define INT64_SUPPORTED // Remove this if the compiler doesn't support 64-bit integers
#endif
// #define MT11213A // Uses the Mersenne Twister 11213A (otherwise 19937 is used).
// Choose which version of Mersenne Twister you want:
#ifdef MT11213A
// Define constants for type MT11213A:
#define MERS_N 351
#define MERS_M 175
#define MERS_R 19
#define MERS_U 11
#define MERS_S 7
#define MERS_T 15
#define MERS_L 17
#define MERS_A 0xE4BD75F5
#define MERS_B 0x655E5280
#define MERS_C 0xFFD58000
#else
// or constants for type MT19937:
#define MERS_N 624
#define MERS_M 397
#define MERS_R 31
#define MERS_U 11
#define MERS_S 7
#define MERS_T 15
#define MERS_L 18
#define MERS_A 0x9908B0DF
#define MERS_B 0x9D2C5680
#define MERS_C 0xEFC60000
#endif
/**
* Basic initialization procedure.
* @param seed int
*/
void init_mersenne(int seed);
/**
* Initialize and seed generator.
* @param seed int
*/
void set_seed(int seed);
/**
* Seed by more than 32 bits,
* @param seeds[] int const
* @param nseeds int
*/
void set_seed_array(int const seeds[], int nseeds);
/**
* Generates 32 random bits,
*/
uint32_t get_random_bits();
/**
* Generates random float number in the interval 0 <= x < 1
*/
double get_frandom();
/**
* Generates random double number in the interval 0 < x < 1
* by shifting value from get_frandom by 2^-33
*/
double get_frandom_2();
/**
* Generates random integer in the interval min <= x <= max
* Relative error on frequencies < 2^-32
* @param min int
* @param max int
*/
int get_irandom(int min, int max);
/**
* Output random integer in the interval min <= x <= max
* Each output value has exactly the same probability.
* This is obtained by rejecting certain bit values so that the number
* of possible bit values is divisible by the interval length
* @param min int
* @param max int
*/
int get_irandomx(int min, int max); // Output random integer, exact
/**
* Output two random floats with mean 0 and std dev 1 given
* two floats from a uniform distribution in [0,1]
* uses the Box-Muller transformation
*
* @param y1 *float
* @param y2 *float
* @param x1 float
* @param x2 float
*/
int random_g(double *y1,double *y2 );
#endif