QuantLib
: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
ql
math
randomnumbers
knuthuniformrng.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) 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
/*! \file knuthuniformrng.hpp
21
\brief Knuth uniform random number generator
22
*/
23
24
#ifndef quantlib_knuth_uniform_rng_h
25
#define quantlib_knuth_uniform_rng_h
26
27
#include <
ql/methods/montecarlo/sample.hpp
>
28
#include <vector>
29
30
namespace
QuantLib
{
31
32
//! Uniform random number generator
33
/*! Random number generator by Knuth.
34
For more details see Knuth, Seminumerical Algorithms,
35
3rd edition, Section 3.6.
36
\note This is <b>not</b> Knuth's original implementation which
37
is available at
38
http://www-cs-faculty.stanford.edu/~knuth/programs.html,
39
but rather a slightly modified version wrapped in a C++ class.
40
Such modifications did not affect the code but only the data
41
structures used, which were converted to their standard C++
42
equivalents.
43
*/
44
class
KnuthUniformRng
{
45
public
:
46
typedef
Sample<Real>
sample_type
;
47
/*! if the given seed is 0, a random seed will be chosen
48
based on clock() */
49
explicit
KnuthUniformRng
(
long
seed = 0);
50
/*! returns a sample with weight 1.0 containing a random number
51
uniformly chosen from (0.0,1.0) */
52
sample_type
next
()
const
;
53
private
:
54
static
const
int
KK
,
LL
,
TT
,
QUALITY
;
55
mutable
std::vector<double>
ranf_arr_buf
;
56
mutable
size_t
ranf_arr_ptr
,
ranf_arr_sentinel
;
57
mutable
std::vector<double>
ran_u
;
58
double
mod_sum
(
double
x,
double
y
)
const
;
59
bool
is_odd
(
int
s
)
const
;
60
void
ranf_start
(
long
seed);
61
void
ranf_array
(std::vector<double>& aa,
int
n
)
const
;
62
double
ranf_arr_cycle
()
const
;
63
};
64
65
66
// inline definitions
67
68
inline
KnuthUniformRng::sample_type
KnuthUniformRng::next
()
const
{
69
double
result = (
ranf_arr_ptr
!=
ranf_arr_sentinel
?
70
ranf_arr_buf
[
ranf_arr_ptr
++] :
71
ranf_arr_cycle
());
72
return
{result, 1.0};
73
}
74
75
inline
double
KnuthUniformRng::mod_sum
(
double
x,
double
y
)
const
{
76
return
(x+
y
)-int(x+
y
);
77
}
78
79
inline
bool
KnuthUniformRng::is_odd
(
int
s
)
const
{
80
return
(
s
&1) != 0;
81
}
82
83
}
84
85
86
#endif
y
Real y
Definition:
andreasenhugevolatilityinterpl.cpp:46
n
Size n
Definition:
andreasenhugevolatilityinterpl.cpp:47
QuantLib::KnuthUniformRng
Uniform random number generator.
Definition:
knuthuniformrng.hpp:44
QuantLib::KnuthUniformRng::ranf_array
void ranf_array(std::vector< double > &aa, int n) const
Definition:
knuthuniformrng.cpp:75
QuantLib::KnuthUniformRng::next
sample_type next() const
Definition:
knuthuniformrng.hpp:68
QuantLib::KnuthUniformRng::KK
static const int KK
Definition:
knuthuniformrng.hpp:54
QuantLib::KnuthUniformRng::ranf_arr_cycle
double ranf_arr_cycle() const
Definition:
knuthuniformrng.cpp:84
QuantLib::KnuthUniformRng::LL
static const int LL
Definition:
knuthuniformrng.hpp:54
QuantLib::KnuthUniformRng::ranf_start
void ranf_start(long seed)
Definition:
knuthuniformrng.cpp:36
QuantLib::KnuthUniformRng::is_odd
bool is_odd(int s) const
Definition:
knuthuniformrng.hpp:79
QuantLib::KnuthUniformRng::QUALITY
static const int QUALITY
Definition:
knuthuniformrng.hpp:54
QuantLib::KnuthUniformRng::ranf_arr_ptr
size_t ranf_arr_ptr
Definition:
knuthuniformrng.hpp:56
QuantLib::KnuthUniformRng::TT
static const int TT
Definition:
knuthuniformrng.hpp:54
QuantLib::KnuthUniformRng::mod_sum
double mod_sum(double x, double y) const
Definition:
knuthuniformrng.hpp:75
QuantLib::KnuthUniformRng::sample_type
Sample< Real > sample_type
Definition:
knuthuniformrng.hpp:46
QuantLib::KnuthUniformRng::ranf_arr_sentinel
size_t ranf_arr_sentinel
Definition:
knuthuniformrng.hpp:56
QuantLib::KnuthUniformRng::ran_u
std::vector< double > ran_u
Definition:
knuthuniformrng.hpp:57
QuantLib::KnuthUniformRng::ranf_arr_buf
std::vector< double > ranf_arr_buf
Definition:
knuthuniformrng.hpp:55
QuantLib
Definition:
any.hpp:35
s
Real s
Definition:
perturbativebarrieroptionengine.cpp:1488
sample.hpp
weighted sample
QuantLib::Sample
weighted sample
Definition:
sample.hpp:35
Generated by
Doxygen
1.9.5