QuantLib
: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
ql
math
randomnumbers
seedgenerator.cpp
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) 2004 Ferdinando Ametrano
5
6
This file is part of QuantLib, a free-software/open-source library
7
for financial quantitative analysts and developers - http://quantlib.org/
8
9
QuantLib is free software: you can redistribute it and/or modify it
10
under the terms of the QuantLib license. You should have received a
11
copy of the license along with this program; if not, please email
12
<quantlib-dev@lists.sf.net>. The license is also available online at
13
<http://quantlib.org/license.shtml>.
14
15
This program is distributed in the hope that it will be useful, but WITHOUT
16
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
FOR A PARTICULAR PURPOSE. See the license for more details.
18
*/
19
20
#include <
ql/math/randomnumbers/seedgenerator.hpp
>
21
#include <ctime>
22
#if defined(BOOST_NO_STDC_NAMESPACE)
23
namespace
std
{ using ::time; }
24
#endif
25
26
namespace
QuantLib
{
27
28
// we need to prevent rng from being default-initialized
29
SeedGenerator::SeedGenerator
() : rng_(42UL) {
30
initialize
();
31
}
32
33
void
SeedGenerator::initialize
() {
34
35
// firstSeed is chosen based on clock() and used for the first rng
36
auto
firstSeed = (
unsigned
long)(std::time(
nullptr
));
37
MersenneTwisterUniformRng
first(firstSeed);
38
39
// secondSeed is as random as it could be
40
// feel free to suggest improvements
41
unsigned
long
secondSeed = first.
nextInt32
();
42
43
MersenneTwisterUniformRng
second(secondSeed);
44
45
// use the second rng to initialize the final one
46
unsigned
long
skip = second.
nextInt32
() % 1000;
47
std::vector<unsigned long> init(4);
48
init[0]=second.
nextInt32
();
49
init[1]=second.
nextInt32
();
50
init[2]=second.
nextInt32
();
51
init[3]=second.
nextInt32
();
52
53
rng_
=
MersenneTwisterUniformRng
(init);
54
55
for
(
unsigned
long
i=0; i<skip ; i++)
56
rng_
.
nextInt32
();
57
}
58
59
unsigned
long
SeedGenerator::get
() {
60
return
rng_
.
nextInt32
();
61
}
62
63
}
QuantLib::MersenneTwisterUniformRng
Uniform random number generator.
Definition:
mt19937uniformrng.hpp:41
QuantLib::MersenneTwisterUniformRng::nextInt32
unsigned long nextInt32() const
return a random integer in the [0,0xffffffff]-interval
Definition:
mt19937uniformrng.hpp:60
QuantLib::SeedGenerator::initialize
void initialize()
Definition:
seedgenerator.cpp:33
QuantLib::SeedGenerator::rng_
MersenneTwisterUniformRng rng_
Definition:
seedgenerator.hpp:45
QuantLib::SeedGenerator::get
unsigned long get()
Definition:
seedgenerator.cpp:59
QuantLib::SeedGenerator::SeedGenerator
SeedGenerator()
Definition:
seedgenerator.cpp:29
QuantLib
Definition:
any.hpp:35
std
STL namespace.
seedgenerator.hpp
Random seed generator.
Generated by
Doxygen
1.9.5