blitz Version 1.0.2
Loading...
Searching...
No Matches
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
37namespace 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
46typedef 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
53struct sharedState { };
56
57typedef 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
67template<typename IRNG, typename state>
69};
70
71template<typename IRNG>
73
74public:
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
87protected:
88 static IRNG irng_;
89};
90
91template<typename IRNG>
93
94template<typename IRNG>
96
97public:
99 IRNGWrapper(unsigned int i) : irng_(MersenneTwisterCreator::create(i)) {};
100
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
113protected:
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
IRNGWrapper(unsigned int i)
Definition: default.h:99
T_state getState() const
Definition: default.h:108
IRNG::T_state T_state
Definition: default.h:107
std::string getStateString() const
Definition: default.h:109
IRNG irng_
Definition: default.h:114
void setState(const T_state &s)
Definition: default.h:110
void seed(IRNG_int x)
Definition: default.h:101
void setState(const std::string &s)
Definition: default.h:111
void seed(std::vector< IRNG_int > x)
Definition: default.h:104
void seed(IRNG_int x)
Definition: default.h:75
T_state getState() const
Definition: default.h:82
void seed(std::vector< IRNG_int > x)
Definition: default.h:78
std::string getStateString() const
Definition: default.h:83
static IRNG irng_
Definition: default.h:88
void setState(const T_state &s)
Definition: default.h:84
IRNG::T_state T_state
Definition: default.h:81
void setState(const std::string &s)
Definition: default.h:85
Definition: default.h:68
This class creates MersenneTwisters with different parameters indexed by and ID number.
Definition: mt.h:323
Definition: mt.h:76
Definition: beta.h:50
float defaultType
Definition: default.h:46
sharedState defaultState
Definition: default.h:55
unsigned int IRNG_int
Definition: default.h:57
MersenneTwister defaultIRNG
Definition: default.h:120
Definition: default.h:54
Definition: default.h:53