[1022] | 1 | // -*- C++ -*- |
---|
| 2 | /*************************************************************************** |
---|
| 3 | * random/default.h Default IRNG wrapper class |
---|
| 4 | * |
---|
| 5 | * $Id$ |
---|
| 6 | * |
---|
| 7 | * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org> |
---|
| 8 | * |
---|
| 9 | * This file is a part of Blitz. |
---|
| 10 | * |
---|
| 11 | * Blitz is free software: you can redistribute it and/or modify |
---|
| 12 | * it under the terms of the GNU Lesser General Public License |
---|
| 13 | * as published by the Free Software Foundation, either version 3 |
---|
| 14 | * of the License, or (at your option) any later version. |
---|
| 15 | * |
---|
| 16 | * Blitz is distributed in the hope that it will be useful, |
---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 19 | * GNU Lesser General Public License for more details. |
---|
| 20 | * |
---|
| 21 | * You should have received a copy of the GNU Lesser General Public |
---|
| 22 | * License along with Blitz. If not, see <http://www.gnu.org/licenses/>. |
---|
| 23 | * |
---|
| 24 | * Suggestions: blitz-devel@lists.sourceforge.net |
---|
| 25 | * Bugs: blitz-support@lists.sourceforge.net |
---|
| 26 | * |
---|
| 27 | * For more information, please see the Blitz++ Home Page: |
---|
| 28 | * https://sourceforge.net/projects/blitz/ |
---|
| 29 | * |
---|
| 30 | ***************************************************************************/ |
---|
| 31 | |
---|
| 32 | #ifndef BZ_RANDOM_DEFAULT_H |
---|
| 33 | #define BZ_RANDOM_DEFAULT_H |
---|
| 34 | |
---|
| 35 | #include <random/mt.h> |
---|
| 36 | |
---|
| 37 | BZ_NAMESPACE(ranlib) |
---|
| 38 | |
---|
| 39 | // Some terminology: |
---|
| 40 | // IRNG = Integer Random Number Generator. IRNGs generate random |
---|
| 41 | // integers, which are used to create floating-point random |
---|
| 42 | // numbers. |
---|
| 43 | // RNG = Random Number Generator. RNGs use IRNGs to create floating- |
---|
| 44 | // point random numbers following desired distributions. |
---|
| 45 | |
---|
| 46 | typedef float defaultType; |
---|
| 47 | |
---|
| 48 | // These are type tags. A RNG with sharedState shares an IRNG |
---|
| 49 | // with other RNGs. An RNG with independentState |
---|
| 50 | // contains its own IRNG. Generally, sharedState RNGs should be |
---|
| 51 | // used. |
---|
| 52 | |
---|
| 53 | struct sharedState { }; |
---|
| 54 | struct independentState { }; |
---|
| 55 | typedef sharedState defaultState; |
---|
| 56 | |
---|
| 57 | typedef unsigned int IRNG_int; |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | // IRNGWrapper handles shared and independent state IRNGs. |
---|
| 61 | // If a class inherits from IRNGWrapper<IRNG,sharedState>, |
---|
| 62 | // it gets a static IRNG (i.e. the IRNG state is shared among |
---|
| 63 | // all RNGs); if it inherits from IRNGWrapper<IRNG,independentState>, |
---|
| 64 | // it gets an independent IRNG (the IRNG state is encapsulated |
---|
| 65 | // in the RNG, and is not shared among RNGs). |
---|
| 66 | |
---|
| 67 | template<typename IRNG, typename state> |
---|
| 68 | class IRNGWrapper { |
---|
| 69 | }; |
---|
| 70 | |
---|
| 71 | template<typename IRNG> |
---|
| 72 | class IRNGWrapper<IRNG,sharedState> { |
---|
| 73 | |
---|
| 74 | public: |
---|
| 75 | void seed(IRNG_int x) |
---|
| 76 | { irng_.seed(x); } |
---|
| 77 | |
---|
| 78 | void seed(std::vector<IRNG_int> x) |
---|
| 79 | { irng_.seed(x); } |
---|
| 80 | |
---|
| 81 | typedef typename IRNG::T_state T_state; |
---|
| 82 | T_state getState() const { return irng_.getState(); } |
---|
| 83 | std::string getStateString() const { return irng_.getStateString(); } |
---|
| 84 | void setState(const T_state& s) { irng_.setState(s); } |
---|
| 85 | void setState(const std::string& s) { irng_.setState(s); } |
---|
| 86 | |
---|
| 87 | protected: |
---|
| 88 | static IRNG irng_; |
---|
| 89 | }; |
---|
| 90 | |
---|
| 91 | template<typename IRNG> |
---|
| 92 | IRNG IRNGWrapper<IRNG,sharedState>::irng_; |
---|
| 93 | |
---|
| 94 | template<typename IRNG> |
---|
| 95 | class IRNGWrapper<IRNG,independentState> { |
---|
| 96 | |
---|
| 97 | public: |
---|
| 98 | IRNGWrapper() {}; |
---|
| 99 | IRNGWrapper(unsigned int i) : irng_(MersenneTwisterCreator::create(i)) {}; |
---|
| 100 | |
---|
| 101 | void seed(IRNG_int x) |
---|
| 102 | { irng_.seed(x); } |
---|
| 103 | |
---|
| 104 | void seed(std::vector<IRNG_int> x) |
---|
| 105 | { irng_.seed(x); } |
---|
| 106 | |
---|
| 107 | typedef typename IRNG::T_state T_state; |
---|
| 108 | T_state getState() const { return irng_.getState(); } |
---|
| 109 | std::string getStateString() const { return irng_.getStateString(); } |
---|
| 110 | void setState(const T_state& s) { irng_.setState(s); } |
---|
| 111 | void setState(const std::string& s) { irng_.setState(s); } |
---|
| 112 | |
---|
| 113 | protected: |
---|
| 114 | IRNG irng_; |
---|
| 115 | }; |
---|
| 116 | |
---|
| 117 | // defaultIRNG is a type alias for the default Integer Random |
---|
| 118 | // Number Generator (IRNG). |
---|
| 119 | |
---|
| 120 | typedef MersenneTwister defaultIRNG; |
---|
| 121 | |
---|
| 122 | BZ_NAMESPACE_END |
---|
| 123 | |
---|
| 124 | #endif // BZ_RANDOM_DEFAULT_H |
---|
| 125 | |
---|