TF++, Miscellaneous classes and modules in C++:
rng.cc
Go to the documentation of this file.
1 
35 #define TF_RNG_CC_VERSION \
36  "TF_RNG_CC V1.1"
37 
38 #include <iostream>
39 #include <tfxx/rng.h>
40 #include <time.h>
41 #include <gsl/gsl_rng.h>
42 #include <string>
43 
44 namespace tfxx {
45 
46  namespace numeric {
47 
48  /* ---------------------------------------------------------------------- */
49  // class RNGgaussian
50 
51  // constructor
53  const double& mean,
54  const char* type):
55  Mstd(std), Mmean(mean)
56  {
57  if (std::string(type) == "default")
58  {
59  gsl_rng_env_setup();
60  const gsl_rng_type* T=gsl_rng_default;
61  MR=gsl_rng_alloc(T);
62  TFXX_assert(MR!=NULL, "gsl_rng_alloc(T) failed to initialize RNG");
63  }
64  else
65  {
66  const gsl_rng_type* T=NULL;
67  const gsl_rng_type **t, **t0;
68  t0 = gsl_rng_types_setup ();
69 
70  for (t = t0; *t != 0; t++)
71  {
72  if (std::string((*t)->name)==std::string(type)) { T=(*t); }
73  }
74  TFXX_Xassert(T!=NULL, type, UTException);
75  MR=gsl_rng_alloc(T);
76  TFXX_assert(MR!=NULL, "gsl_rng_alloc(T) failed to initialize RNG");
77  }
78  this->set(gsl_rng_default_seed);
79  }
80 
81  /* ---------------------------------------------------------------------- */
82 
83  // destructor
85  {
86  gsl_rng_free(MR);
87  }
88 
89  /* ---------------------------------------------------------------------- */
90 
91  // return value
92  double RNGgaussian::value() const
93  { return(Mmean+Mstd*gsl_ran_ugaussian(MR)); }
94 
95  /* ---------------------------------------------------------------------- */
96 
97  // return value
98  double RNGgaussian::operator()() const
99  { return(this->value()); }
100 
101  /* ---------------------------------------------------------------------- */
102 
103  // set seed
104  void RNGgaussian::set(const unsigned long int& seed)
105  {
106  gsl_rng_set(MR, seed);
107  Mseed=seed;
108  }
109 
110  /* ---------------------------------------------------------------------- */
111 
112  // set seed using time
114  { this->set(time(0)); }
115 
116  /* ---------------------------------------------------------------------- */
117 
122  void RNGgaussian::rng_list_types(std::ostream& os)
123  {
124  const gsl_rng_type **t, **t0;
125 
126  t0 = gsl_rng_types_setup ();
127 
128  for (t = t0; *t != 0; t++)
129  {
130  os << std::string((*t)->name) << std::endl;
131  }
132 
133  } // RNGgaussian::rng_list_types(std::ostream* os)
134 
135  /* ====================================================================== */
136 
138  const char* file,
139  const int& line,
140  const char* condition):
141  TBase("unkown RNG type requested", file, line, condition),
142  Mtype(type)
143  {
144  if (this->report_on_construct_is_true())
145  {
146  std::cerr << " Requested RNG type: " << Mtype << std::endl;
147  }
148  } // RNGgaussian::UTException::UTException
149 
150  /* ====================================================================== */
151 
153  {
154 "The recommended random number generator type is \"ranlxd2\"\n"
155 "\n"
156 "Quotation from \n"
157 "https://www.gnu.org/software/gsl/doc/html/index.html\n"
158 "https://www.gnu.org/software/gsl/doc/html/rng.html#random-number-generator-algorithms\n"
159 "\n"
160 "The following generators are recommended for use in simulation. They have\n"
161 "extremely long periods, low correlation and pass most statistical tests.\n"
162 "For the most reliable source of uncorrelated numbers, the second-generation\n"
163 "RANLUX generators have the strongest proof of randomness.\n"
164 "\n"
165 "gsl_rng_ranlxd1\n"
166 "gsl_rng_ranlxd2\n"
167 "\n"
168 " These generators produce double precision output (48 bits) from the\n"
169 " RANLXS generator. The library provides two luxury levels ranlxd1 and\n"
170 " ranlxd2, in increasing order of strength.\n"
171 "\n"
172 "gsl_rng_ranlux\n"
173 "gsl_rng_ranlux389\n"
174 "\n"
175 " The ranlux generator is an implementation of the original algorithm\n"
176 " developed by Luscher. It uses a lagged-fibonacci-with-skipping\n"
177 " algorithm to produce “luxury random numbers”. It is a 24-bit\n"
178 " generator, originally designed for single-precision IEEE floating\n"
179 " point numbers. This implementation is based on integer arithmetic,\n"
180 " while the second-generation versions RANLXS and RANLXD described\n"
181 " above provide floating-point implementations which will be faster\n"
182 " on many platforms. The period of the generator is about 10^{171}.\n"
183 " The algorithm has mathematically proven properties and it can\n"
184 " provide truly decorrelated numbers at a known level of randomness.\n"
185 " The default level of decorrelation recommended by Luscher is\n"
186 " provided by gsl_rng_ranlux, while gsl_rng_ranlux389 gives the\n"
187 " highest level of randomness, with all 24 bits decorrelated. Both\n"
188 " types of generator use 24 words of state per generator.\n"
189 "\n"
190 " For more information see,\n"
191 "\n"
192 " * M. Luscher, “A portable high-quality random number generator for\n"
193 " lattice field theory calculations”, Computer Physics Communications,\n"
194 " 79 (1994) 100–110.\n"
195 " DOI: 10.1016/0010-4655(94)90232-1.\n"
196 " * F. James, “RANLUX: A Fortran implementation of the high-quality\n"
197 " pseudo-random number generator of Luscher”, Computer Physics\n"
198 " Communications, 79 (1994) 111–114\n"
199 " DOI: 10.1016/0010-4655(94)90233-X\n"
200  }; // RNGgaussian::comment_gsl_rng_ranlux
201 
202  } // namespace numeric
203 
204 } // namespace tfxx
205 
206 /* ----- END OF rng.cc ----- */
RNGgaussian(const double &std=1., const double &mean=0., const char *type="default")
initialize random number generator
Definition: rng.cc:52
#define TFXX_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:175
void set()
use time as seed value
Definition: rng.cc:113
STL namespace.
UTException(const char *type, const char *file, const int &line, const char *condition)
Create with message, failed assertion, and code position.
Definition: rng.cc:137
double Mstd
store standard deviation and mean value
Definition: rng.h:107
double value() const
returns a random number
Definition: rng.cc:92
unsigned long int seed() const
return last seed value
Definition: rng.h:96
std::string type() const
return type of random number generator
Definition: rng.h:94
const char * Mtype
pointer name string of requested RNG type
Definition: rng.h:132
bool report_on_construct_is_true() const
Definition: error.h:108
static void rng_list_types(std::ostream &os)
print list of random number generators to stream.
Definition: rng.cc:122
exception class for RNG indicating request for unkown type
Definition: rng.h:118
#define TFXX_Xassert(C, M, E)
Check an assertion and report by throwing an exception.
Definition: error.h:166
unsigned long int Mseed
memorize last seed value
Definition: rng.h:111
static const char * comment_gsl_rng_ranlux
comment on GSL RNGs
Definition: rng.h:104
double operator()() const
returns a random number
Definition: rng.cc:98
Base class for exceptions.
Definition: error.h:78
gsl_rng * MR
pointer to RNG
Definition: rng.h:109
Namespace containing all code of library libtfxx.