blitz  Version 1.0.2
default.h
Go to the documentation of this file.
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 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 { };
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>
93 
94 template<typename IRNG>
96 
97 public:
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 
121 
122 }
123 
124 #endif // BZ_RANDOM_DEFAULT_H
125 
Definition: beta.h:50
static IRNG irng_
Definition: default.h:88
void setState(const T_state &s)
Definition: default.h:110
_bz_global blitz::IndexPlaceholder< 0 > i
Definition: indexexpr.h:256
std::string getStateString() const
Definition: default.h:83
IRNG::T_state T_state
Definition: default.h:81
IRNGWrapper(unsigned int i)
Definition: default.h:99
This class creates MersenneTwisters with different parameters indexed by and ID number.
Definition: mt.h:322
Definition: default.h:68
float defaultType
Definition: default.h:46
void setState(const std::string &s)
Definition: default.h:85
std::string getStateString() const
Definition: default.h:109
void seed(IRNG_int x)
Definition: default.h:75
void setState(const T_state &s)
Definition: default.h:84
IRNG irng_
Definition: default.h:114
void setState(const std::string &s)
Definition: default.h:111
unsigned int IRNG_int
Definition: default.h:57
T_state getState() const
Definition: default.h:108
Definition: default.h:53
MersenneTwister defaultIRNG
Definition: default.h:120
void seed(std::vector< IRNG_int > x)
Definition: default.h:78
IRNG::T_state T_state
Definition: default.h:107
Definition: mt.h:75
void seed(IRNG_int x)
Definition: default.h:101
_bz_global blitz::IndexPlaceholder< 10 > s
Definition: indexexpr.h:266
Definition: default.h:54
void seed(std::vector< IRNG_int > x)
Definition: default.h:104
sharedState defaultState
Definition: default.h:55
T_state getState() const
Definition: default.h:82