| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2009 Klaus Spanderen |
| 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 ranluxuniformrng.hpp |
| 21 | \brief "Luxury" random number generator. |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_ranlux_uniform_rng_h |
| 25 | #define quantlib_ranlux_uniform_rng_h |
| 26 | |
| 27 | #include <ql/methods/montecarlo/sample.hpp> |
| 28 | |
| 29 | #include <random> |
| 30 | |
| 31 | namespace QuantLib { |
| 32 | |
| 33 | //! Uniform random number generator |
| 34 | /*! M. Luescher's "luxury" random number generator |
| 35 | |
| 36 | Implementation is a proxy for the corresponding boost random |
| 37 | number generator. For more detail see the boost documentation and: |
| 38 | M.Luescher, A portable high-quality random number generator for |
| 39 | lattice field theory simulations, Comp. Phys. Comm. 79 (1994) 100 |
| 40 | |
| 41 | Available luxury levels: |
| 42 | Ranlux3: Any theoretically possible correlations have very small change |
| 43 | of being observed. |
| 44 | Ranlux4: highest possible luxury. |
| 45 | */ |
| 46 | template <std::size_t P, std::size_t R> |
| 47 | class Ranlux64UniformRng { |
| 48 | public: |
| 49 | typedef Sample<Real> sample_type; |
| 50 | |
| 51 | explicit Ranlux64UniformRng(std::uint_fast64_t seed = 19780503U) |
| 52 | : ranlux_(ranlux64_base_01(seed)) {} |
| 53 | |
| 54 | sample_type next() const { return {ranlux_()*nx, 1.0}; } |
| 55 | |
| 56 | private: |
| 57 | const double nx = 1.0/(std::uint_fast64_t(1) << 48); |
| 58 | typedef std::subtract_with_carry_engine<std::uint_fast64_t, 48, 10, 24> |
| 59 | ranlux64_base_01; |
| 60 | mutable std::discard_block_engine<ranlux64_base_01, P, R> ranlux_; |
| 61 | }; |
| 62 | |
| 63 | typedef Ranlux64UniformRng<223, 24> Ranlux3UniformRng; |
| 64 | typedef Ranlux64UniformRng<389, 24> Ranlux4UniformRng; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | #endif |
| 69 | |