/src/quantlib/ql/math/randomnumbers/seedgenerator.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 0 | SeedGenerator::SeedGenerator() : rng_(42UL) { |
30 | 0 | initialize(); |
31 | 0 | } |
32 | | |
33 | 0 | void SeedGenerator::initialize() { |
34 | | |
35 | | // firstSeed is chosen based on clock() and used for the first rng |
36 | 0 | auto firstSeed = (unsigned long)(std::time(nullptr)); |
37 | 0 | MersenneTwisterUniformRng first(firstSeed); |
38 | | |
39 | | // secondSeed is as random as it could be |
40 | | // feel free to suggest improvements |
41 | 0 | unsigned long secondSeed = first.nextInt32(); |
42 | |
|
43 | 0 | MersenneTwisterUniformRng second(secondSeed); |
44 | | |
45 | | // use the second rng to initialize the final one |
46 | 0 | unsigned long skip = second.nextInt32() % 1000; |
47 | 0 | std::vector<unsigned long> init(4); |
48 | 0 | init[0]=second.nextInt32(); |
49 | 0 | init[1]=second.nextInt32(); |
50 | 0 | init[2]=second.nextInt32(); |
51 | 0 | init[3]=second.nextInt32(); |
52 | |
|
53 | 0 | rng_ = MersenneTwisterUniformRng(init); |
54 | |
|
55 | 0 | for (unsigned long i=0; i<skip ; i++) |
56 | 0 | rng_.nextInt32(); |
57 | 0 | } |
58 | | |
59 | 0 | unsigned long SeedGenerator::get() { |
60 | 0 | return rng_.nextInt32(); |
61 | 0 | } |
62 | | |
63 | | } |