QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
mt19937uniformrng.hpp
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
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
31namespace QuantLib {
32
34
42 private:
43 static const Size N = 624; // state size
44 static const Size M = 397; // shift size
45 public:
49 explicit MersenneTwisterUniformRng(unsigned long seed = 0);
51 const std::vector<unsigned long>& seeds);
54 sample_type next() const { return {nextReal(), 1.0}; }
56 Real nextReal() const {
57 return (Real(nextInt32()) + 0.5)/4294967296.0;
58 }
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
Definition: sample.hpp:35