QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
mersenne-twister.h
Go to the documentation of this file.
1 /*
2  * The Mersenne Twister pseudo-random number generator (PRNG)
3  *
4  * This is an implementation of fast PRNG called MT19937,
5  * meaning it has a period of 2^19937-1, which is a Mersenne
6  * prime.
7  *
8  * This PRNG is fast and suitable for non-cryptographic code.
9  * For instance, it would be perfect for Monte Carlo simulations,
10  * etc.
11  *
12  * This code has been designed as a drop-in replacement for libc rand and
13  * srand(). If you need to mix them, you should encapsulate this code in a
14  * namespace.
15  *
16  * Written by Christian Stigen Larsen
17  * http://csl.name
18  *
19  * Distributed under the modified BSD license.
20  *
21  * 2015-02-17
22  */
23 
24 #ifndef MERSENNE_TWISTER_H
25 #define MERSENNE_TWISTER_H
26 
27 #ifndef _MSC_VER
28 #include <stdint.h>
29 #else
30 typedef __int32 int32_t;
31 typedef unsigned __int32 uint32_t;
32 typedef __int64 int64_t;
33 typedef unsigned __int64 uint64_t;
34 #endif
35 #include <limits>
36 
37 #ifdef __cplusplus
38 extern "C"
39 {
40 #endif
41 
42  /*
43  * Maximum number you can get from rand().
44  */
45 #define MD_RAND_MAX std::numeric_limits<int32_t>::max()
46 
47  /*
48  * Initialize the number generator with given seed.
49  * (LIBC REPLACEMENT FUNCTION)
50  */
51  void mt_srand( unsigned seed_value );
52 
53  /*
54  * Extract a pseudo-random integer in the range 0 ... MD_RAND_MAX.
55  * (LIBC REPLACEMENT FUNCTION)
56  */
57  int mt_rand();
58 
59  /*
60  * Extract a pseudo-random unsigned 32-bit integer in the range 0 ... MD_UINT32_MAX
61  */
62  uint32_t rand_u32();
63 
64  /*
65  * Combine two unsigned 32-bit pseudo-random numbers into one 64-bit
66  */
67  uint64_t rand_u64();
68 
69  /*
70  * Initialize Mersenne Twister with given seed value.
71  */
72  void seed( uint32_t seed_value );
73 
74  /*
75  * Return a random float in the CLOSED range [0, 1]
76  * Mnemonic: randf_co = random float 0=closed 1=closed
77  */
78  float randf_cc();
79 
80  /*
81  * Return a random float in the OPEN range [0, 1>
82  * Mnemonic: randf_co = random float 0=closed 1=open
83  */
84  float randf_co();
85 
86  /*
87  * Return a random float in the OPEN range <0, 1>
88  * Mnemonic: randf_oo = random float 0=open 1=open
89  */
90  float randf_oo();
91 
92  /*
93  * Return a random double in the CLOSED range [0, 1]
94  * Mnemonic: randd_co = random double 0=closed 1=closed
95  */
96  double randd_cc();
97 
98  /*
99  * Return a random double in the OPEN range [0, 1>
100  * Mnemonic: randd_co = random double 0=closed 1=open
101  */
102  double randd_co();
103 
104  /*
105  * Return a random double in the OPEN range <0, 1>
106  * Mnemonic: randd_oo = random double 0=open 1=open
107  */
108  double randd_oo();
109 
110 #ifdef __cplusplus
111 } // extern "C"
112 #endif
113 
114 #endif // MERSENNE_TWISTER_H