Coverage Report

Created: 2026-09-28 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/math/randomnumbers/randomsequencegenerator.hpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2003 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
 <https://www.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 randomsequencegenerator.hpp
21
    \brief Random sequence generator based on a pseudo-random number generator
22
*/
23
24
#ifndef quantlib_random_sequence_generator_h
25
#define quantlib_random_sequence_generator_h
26
27
#include <ql/methods/montecarlo/sample.hpp>
28
#include <ql/errors.hpp>
29
#include <vector>
30
31
namespace QuantLib {
32
33
    //! Random sequence generator based on a pseudo-random number generator
34
    /*! Random sequence generator based on a pseudo-random number
35
        generator RNG.
36
37
        Class RNG must implement the following interface:
38
        \code
39
            RNG::sample_type RNG::next() const;
40
        \endcode
41
        If a client of this class wants to use the nextInt32Sequence method,
42
        class RNG must also implement
43
        \code
44
            unsigned long RNG::nextInt32() const;
45
        \endcode
46
47
        \warning do not use with low-discrepancy sequence generator.
48
    */
49
    template<class RNG>
50
    class RandomSequenceGenerator {
51
      public:
52
        typedef Sample<std::vector<Real> > sample_type;
53
        RandomSequenceGenerator(Size dimensionality,
54
                                const RNG& rng)
55
0
        : dimensionality_(dimensionality), rng_(rng),
56
0
          sequence_(std::vector<Real> (dimensionality), 1.0),
57
0
          int32Sequence_(dimensionality) {
58
0
          QL_REQUIRE(dimensionality>0, 
59
0
                     "dimensionality must be greater than 0");
60
0
        }
61
62
        /*! if the given seed is 0, a random seed will be chosen
63
            based on clock() */
64
        explicit RandomSequenceGenerator(Size dimensionality,
65
                                         BigNatural seed = 0)
66
636
        : dimensionality_(dimensionality), rng_(seed),
67
636
          sequence_(std::vector<Real> (dimensionality), 1.0),
68
636
          int32Sequence_(dimensionality) {}
69
70
63.6k
        const sample_type& nextSequence() const {
71
63.6k
            sequence_.weight = 1.0;
72
2.19M
            for (Size i=0; i<dimensionality_; i++) {
73
2.13M
                typename RNG::sample_type x(rng_.next());
74
2.13M
                sequence_.value[i] = x.value;
75
2.13M
                sequence_.weight  *= x.weight;
76
2.13M
            }
77
63.6k
            return sequence_;
78
63.6k
        }
79
0
        std::vector<BigNatural> nextInt32Sequence() const {
80
0
            for (Size i=0; i<dimensionality_; i++) {
81
0
                int32Sequence_[i] = rng_.nextInt32();
82
0
            }
83
0
            return int32Sequence_;
84
0
        }
85
0
        const sample_type& lastSequence() const {
86
0
            return sequence_;
87
0
        }
88
636
        Size dimension() const {return dimensionality_;}
89
      private:
90
        Size dimensionality_;
91
        RNG rng_;
92
        mutable sample_type sequence_;
93
        mutable std::vector<BigNatural> int32Sequence_;
94
    };
95
96
}
97
98
99
#endif