blitz Version 0.10
random/default.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /***************************************************************************
00003  * random/default.h       Default IRNG wrapper class
00004  *
00005  * $Id: default.h,v 1.4 2011/03/25 22:41:17 julianc Exp $
00006  *
00007  * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org>
00008  *
00009  * This file is a part of Blitz.
00010  *
00011  * Blitz is free software: you can redistribute it and/or modify 
00012  * it under the terms of the GNU Lesser General Public License
00013  * as published by the Free Software Foundation, either version 3
00014  * of the License, or (at your option) any later version.
00015  *
00016  * Blitz is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public 
00022  * License along with Blitz.  If not, see <http://www.gnu.org/licenses/>.
00023  * 
00024  * Suggestions:          blitz-devel@lists.sourceforge.net
00025  * Bugs:                 blitz-support@lists.sourceforge.net    
00026  *
00027  * For more information, please see the Blitz++ Home Page:
00028  *    https://sourceforge.net/projects/blitz/
00029  *
00030  ***************************************************************************/
00031 
00032 #ifndef BZ_RANDOM_DEFAULT_H
00033 #define BZ_RANDOM_DEFAULT_H
00034 
00035 #include <random/mt.h>
00036 
00037 BZ_NAMESPACE(ranlib)
00038 
00039 // Some terminology:
00040 // IRNG = Integer Random Number Generator.  IRNGs generate random
00041 //        integers, which are used to create floating-point random
00042 //        numbers.
00043 // RNG  = Random Number Generator.  RNGs use IRNGs to create floating-
00044 //        point random numbers following desired distributions.
00045 
00046 typedef float defaultType;
00047 
00048 // These are type tags.  A RNG with sharedState shares an IRNG
00049 // with other RNGs.  An RNG with independentState
00050 // contains its own IRNG.  Generally, sharedState RNGs should be
00051 // used.
00052 
00053 struct sharedState { };
00054 struct independentState { };
00055 typedef sharedState defaultState;
00056 
00057 typedef unsigned int IRNG_int;
00058 
00059 
00060 // IRNGWrapper handles shared and independent state IRNGs.
00061 // If a class inherits from IRNGWrapper<IRNG,sharedState>,
00062 // it gets a static IRNG (i.e. the IRNG state is shared among
00063 // all RNGs); if it inherits from IRNGWrapper<IRNG,independentState>,
00064 // it gets an independent IRNG (the IRNG state is encapsulated
00065 // in the RNG, and is not shared among RNGs).
00066 
00067 template<typename IRNG, typename state>
00068 class IRNGWrapper {
00069 };
00070 
00071 template<typename IRNG>
00072 class IRNGWrapper<IRNG,sharedState> {
00073 
00074 public:
00075     void seed(IRNG_int x)
00076     { irng_.seed(x); }
00077 
00078   void seed(std::vector<IRNG_int> x)
00079     { irng_.seed(x); }
00080 
00081     typedef typename IRNG::T_state T_state;
00082     T_state getState() const { return irng_.getState(); }
00083     std::string getStateString() const { return irng_.getStateString(); }
00084     void setState(const T_state& s) { irng_.setState(s); }
00085     void setState(const std::string& s) { irng_.setState(s); }
00086 
00087 protected:
00088     static IRNG irng_;
00089 };
00090 
00091 template<typename IRNG>
00092 IRNG IRNGWrapper<IRNG,sharedState>::irng_;
00093 
00094 template<typename IRNG>
00095 class IRNGWrapper<IRNG,independentState> {
00096 
00097 public:
00098   IRNGWrapper() {};
00099   IRNGWrapper(unsigned int i) : irng_(MersenneTwisterCreator::create(i)) {};
00100 
00101     void seed(IRNG_int x)
00102     { irng_.seed(x); }
00103 
00104   void seed(std::vector<IRNG_int> x)
00105     { irng_.seed(x); }
00106 
00107     typedef typename IRNG::T_state T_state;
00108     T_state getState() const { return irng_.getState(); }
00109     std::string getStateString() const { return irng_.getStateString(); }
00110     void setState(const T_state& s) { irng_.setState(s); }
00111     void setState(const std::string& s) { irng_.setState(s); }
00112 
00113 protected:
00114     IRNG irng_;
00115 };
00116 
00117 // defaultIRNG is a type alias for the default Integer Random
00118 // Number Generator (IRNG).
00119 
00120 typedef MersenneTwister defaultIRNG;
00121 
00122 BZ_NAMESPACE_END
00123 
00124 #endif // BZ_RANDOM_DEFAULT_H
00125 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines