QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mt19937uniformrng.hpp
Go to the documentation of this file.
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 <http://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
29#include <vector>
30
31namespace 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 */
42 private:
43 static const Size N = 624; // state size
44 static const Size M = 397; // shift size
45 public:
47 /*! if the given seed is 0, a random seed will be chosen
48 based on clock() */
49 explicit MersenneTwisterUniformRng(unsigned long seed = 0);
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 sample_type next() const { return {nextReal(), 1.0}; }
55 //! return a random number in the (0.0, 1.0)-interval
56 Real nextReal() const {
57 return (Real(nextInt32()) + 0.5)/4294967296.0;
58 }
59 //! return a random integer in the [0,0xffffffff]-interval
60 unsigned long nextInt32() const {
61 if (mti==N)
62 twist(); /* generate N words at a time */
63
64 unsigned long y = mt[mti++];
65
66 /* Tempering */
67 y ^= (y >> 11);
68 y ^= (y << 7) & 0x9d2c5680UL;
69 y ^= (y << 15) & 0xefc60000UL;
70 y ^= (y >> 18);
71 return y;
72 }
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
Uniform random number generator.
void seedInitialization(unsigned long seed)
static const unsigned long UPPER_MASK
unsigned long nextInt32() const
return a random integer in the [0,0xffffffff]-interval
static const unsigned long LOWER_MASK
Real nextReal() const
return a random number in the (0.0, 1.0)-interval
static const unsigned long MATRIX_A
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
weighted sample
weighted sample
Definition: sample.hpp:35