/src/quantlib/ql/math/randomnumbers/mt19937uniformrng.hpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2003 Ferdinando Ametrano |
5 | | Copyright (C) 2010 Kakhkhor Abdijalilov |
6 | | |
7 | | This file is part of QuantLib, a free-software/open-source library |
8 | | for financial quantitative analysts and developers - http://quantlib.org/ |
9 | | |
10 | | QuantLib is free software: you can redistribute it and/or modify it |
11 | | under the terms of the QuantLib license. You should have received a |
12 | | copy of the license along with this program; if not, please email |
13 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
14 | | <https://www.quantlib.org/license.shtml>. |
15 | | |
16 | | This program is distributed in the hope that it will be useful, but WITHOUT |
17 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
18 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
19 | | */ |
20 | | |
21 | | /*! \file mt19937uniformrng.hpp |
22 | | \brief Mersenne Twister uniform random number generator |
23 | | */ |
24 | | |
25 | | #ifndef quantlib_mersennetwister_uniform_rng_hpp |
26 | | #define quantlib_mersennetwister_uniform_rng_hpp |
27 | | |
28 | | #include <ql/methods/montecarlo/sample.hpp> |
29 | | #include <vector> |
30 | | |
31 | | namespace QuantLib { |
32 | | |
33 | | //! Uniform random number generator |
34 | | /*! Mersenne Twister random number generator of period 2**19937-1 |
35 | | |
36 | | For more details see http://www.math.keio.ac.jp/matumoto/emt.html |
37 | | |
38 | | \test the correctness of the returned values is tested by |
39 | | checking them against known good results. |
40 | | */ |
41 | | class MersenneTwisterUniformRng { |
42 | | private: |
43 | | static const Size N = 624; // state size |
44 | | static const Size M = 397; // shift size |
45 | | public: |
46 | | typedef Sample<Real> sample_type; |
47 | | /*! if the given seed is 0, a random seed will be chosen |
48 | | based on clock() */ |
49 | | explicit MersenneTwisterUniformRng(unsigned long seed = 0); |
50 | | explicit MersenneTwisterUniformRng( |
51 | | const std::vector<unsigned long>& seeds); |
52 | | /*! returns a sample with weight 1.0 containing a random number |
53 | | in the (0.0, 1.0) interval */ |
54 | 0 | sample_type next() const { return {nextReal(), 1.0}; } |
55 | | //! return a random number in the (0.0, 1.0)-interval |
56 | 0 | Real nextReal() const { |
57 | 0 | return (Real(nextInt32()) + 0.5)/4294967296.0; |
58 | 0 | } |
59 | | //! return a random integer in the [0,0xffffffff]-interval |
60 | 0 | unsigned long nextInt32() const { |
61 | 0 | if (mti==N) |
62 | 0 | twist(); /* generate N words at a time */ |
63 | 0 |
|
64 | 0 | unsigned long y = mt[mti++]; |
65 | 0 |
|
66 | 0 | /* Tempering */ |
67 | 0 | y ^= (y >> 11); |
68 | 0 | y ^= (y << 7) & 0x9d2c5680UL; |
69 | 0 | y ^= (y << 15) & 0xefc60000UL; |
70 | 0 | y ^= (y >> 18); |
71 | 0 | return y; |
72 | 0 | } |
73 | | private: |
74 | | void seedInitialization(unsigned long seed); |
75 | | void twist() const; |
76 | | mutable unsigned long mt[N]; |
77 | | mutable Size mti; |
78 | | static const unsigned long MATRIX_A, UPPER_MASK, LOWER_MASK; |
79 | | }; |
80 | | |
81 | | } |
82 | | |
83 | | |
84 | | #endif |