QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
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
28#include <vector>
29
30namespace 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 */
45 public:
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;
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
69 double result = (ranf_arr_ptr != ranf_arr_sentinel ?
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
Uniform random number generator.
void ranf_array(std::vector< double > &aa, int n) const
sample_type next() const
double mod_sum(double x, double y) const
std::vector< double > ran_u
std::vector< double > ranf_arr_buf
Definition: any.hpp:35
weighted sample
weighted sample
Definition: sample.hpp:35