glucat  0.8.4
random.h
Go to the documentation of this file.
1 #ifndef _GLUCAT_RANDOM_H
2 #define _GLUCAT_RANDOM_H
3 /***************************************************************************
4  GluCat : Generic library of universal Clifford algebra templates
5  random.h : Random number generator with single instance per Scalar_T
6  -------------------
7  begin : 2010-03-28
8  copyright : (C) 2001-2012 by Paul C. Leopardi
9  ***************************************************************************
10 
11  This library is free software: you can redistribute it and/or modify
12  it under the terms of the GNU Lesser General Public License as published
13  by the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  This library 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 License
22  along with this library. If not, see <http://www.gnu.org/licenses/>.
23 
24  ***************************************************************************
25  This library is based on a prototype written by Arvind Raja and was
26  licensed under the LGPL with permission of the author. See Arvind Raja,
27  "Object-oriented implementations of Clifford algebras in C++: a prototype",
28  in Ablamowicz, Lounesto and Parra (eds.)
29  "Clifford algebras with numeric and symbolic computations, Birkhauser, 1996."
30  ***************************************************************************
31  See also Arvind Raja's original header comments and references in glucat.h
32  ***************************************************************************/
33 
34 #if defined(_GLUCAT_USE_GSL_RANDOM)
35 # include <gsl/gsl_rng.h>
36 # include <gsl/gsl_randist.h>
37 #else
38 # include <random>
39 #endif
40 
41 namespace glucat
42 {
44  // Enforce singleton
45  // Reference: A. Alexandrescu, "Modern C++ Design", Chapter 6
46  template< typename Scalar_T >
47  class random_generator
48  {
49  public:
51  static random_generator& generator() { static random_generator g; return g;}
52  private:
56  friend class friend_for_private_destructor;
59  static const unsigned long seed = 19590921UL;
60 #if defined(_GLUCAT_USE_GSL_RANDOM)
61 
62  gsl_rng* gen;
63 
65  gen(gsl_rng_alloc(gsl_rng_mt19937))
66  { gsl_rng_set(this->gen, seed); }
67 
69  { gsl_rng_free(this->gen); }
70 
71  public:
72  Scalar_T uniform()
73  { return Scalar_T(gsl_ran_flat(this->gen, 0.0, 1.0)); }
74  Scalar_T normal()
75  { return Scalar_T(gsl_ran_gaussian(this->gen, 1.0)); }
76 
77 #else
78 
79  std::mt19937 uint_gen;
80  std::uniform_real_distribution<double> uniform_dist;
81  std::normal_distribution<double> normal_dist;
82 
84  uint_gen(), uniform_dist(0.0, 1.0), normal_dist(0.0, 1.0)
85  { this->uint_gen.seed(seed); }
86 
88  { }
89 
90  public:
91  Scalar_T uniform()
92  { return Scalar_T(this->uniform_dist(this->uint_gen)); }
93  Scalar_T normal()
94  { return Scalar_T(this->normal_dist(this->uint_gen)); }
95 
96 #endif
97  };
98 }
99 
100 #endif // _GLUCAT_RANDOM_H
glucat::random_generator::seed
static const unsigned long seed
Definition: random.h:117
glucat::random_generator::normal_dist
std::normal_distribution< double > normal_dist
Definition: random.h:139
glucat::random_generator::uint_gen
std::mt19937 uint_gen
Definition: random.h:137
glucat::random_generator::generator
static random_generator & generator()
Single instance of Random number generator.
Definition: random.h:109
glucat::random_generator::uniform
Scalar_T uniform()
Definition: random.h:149
glucat::random_generator::friend_for_private_destructor
friend class friend_for_private_destructor
Definition: random.h:114
glucat::random_generator::~random_generator
~random_generator()
Definition: random.h:145
glucat::random_generator::uniform_dist
std::uniform_real_distribution< double > uniform_dist
Definition: random.h:138
glucat::random_generator::random_generator
random_generator()
Definition: random.h:141
glucat::random_generator::normal
Scalar_T normal()
Definition: random.h:151
glucat::random_generator::operator=
random_generator & operator=(const random_generator &)
glucat
Definition: clifford_algebra.h:39