/src/quantlib/ql/math/randomnumbers/lecuyeruniformrng.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) 2000, 2001, 2002, 2003 RiskMap srl |
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/lecuyeruniformrng.hpp> |
21 | | #include <ql/math/randomnumbers/seedgenerator.hpp> |
22 | | |
23 | | namespace QuantLib { |
24 | | |
25 | | const long LecuyerUniformRng::m1 = 2147483563L; |
26 | | const long LecuyerUniformRng::a1 = 40014L; |
27 | | const long LecuyerUniformRng::q1 = 53668L; |
28 | | const long LecuyerUniformRng::r1 = 12211L; |
29 | | |
30 | | const long LecuyerUniformRng::m2 = 2147483399L; |
31 | | const long LecuyerUniformRng::a2 = 40692L; |
32 | | const long LecuyerUniformRng::q2 = 52774L; |
33 | | const long LecuyerUniformRng::r2 = 3791L; |
34 | | |
35 | | const int LecuyerUniformRng::bufferSize = 32; |
36 | | |
37 | | // int(1+m1/bufferSize) = int(1+(m1-1)/bufferSize) |
38 | | const long LecuyerUniformRng::bufferNormalizer = 67108862L; |
39 | | |
40 | | const long double LecuyerUniformRng::maxRandom = 1.0-QL_EPSILON; |
41 | | |
42 | | LecuyerUniformRng::LecuyerUniformRng(long seed) |
43 | 0 | : buffer(LecuyerUniformRng::bufferSize) { |
44 | | // Need to prevent seed=0, so use seed=0 to have a "random" seed |
45 | 0 | temp2 = temp1 = (seed != 0 ? seed : SeedGenerator::instance().get()); |
46 | | // Load the shuffle table (after 8 warm-ups) |
47 | 0 | for (int j=bufferSize+7; j>=0; j--) { |
48 | 0 | long k = temp1/q1; |
49 | 0 | temp1 = a1*(temp1-k*q1)-k*r1; |
50 | 0 | if (temp1 < 0) |
51 | 0 | temp1 += m1; |
52 | 0 | if (j < bufferSize) |
53 | 0 | buffer[j] = temp1; |
54 | 0 | } |
55 | 0 | y = buffer[0]; |
56 | 0 | } |
57 | | |
58 | 0 | LecuyerUniformRng::sample_type LecuyerUniformRng::next() const { |
59 | 0 | long k = temp1/q1; |
60 | | // Compute temp1=(a1*temp1) % m1 |
61 | | // without overflows (Schrage's method) |
62 | 0 | temp1 = a1*(temp1-k*q1)-k*r1; |
63 | 0 | if (temp1 < 0) |
64 | 0 | temp1 += m1; |
65 | 0 | k = temp2/q2; |
66 | | // Compute temp2=(a2*temp2) % m2 |
67 | | // without overflows (Schrage's method) |
68 | 0 | temp2 = a2*(temp2-k*q2)-k*r2; |
69 | 0 | if (temp2 < 0) |
70 | 0 | temp2 += m2; |
71 | | // Will be in the range 0..bufferSize-1 |
72 | 0 | int j = y/bufferNormalizer; |
73 | | // Here temp1 is shuffled, temp1 and temp2 are |
74 | | // combined to generate output |
75 | 0 | y = buffer[j]-temp2; |
76 | 0 | buffer[j] = temp1; |
77 | 0 | if (y < 1) |
78 | 0 | y += m1-1; |
79 | 0 | double result = y/double(m1); |
80 | | // users don't expect endpoint values |
81 | 0 | if (result > maxRandom) |
82 | 0 | result = (double) maxRandom; |
83 | 0 | return {result, 1.0}; |
84 | 0 | } |
85 | | |
86 | | } |