glucat 0.12.0
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#include <random>
35
36namespace glucat
37{
39 // Enforce singleton
40 // Reference: A. Alexandrescu, "Modern C++ Design", Chapter 6
41 template< typename Scalar_T >
43 {
44 private:
49 public:
51 static auto generator() -> random_generator& { static random_generator g; return g;}
53 auto operator= (const random_generator&) -> random_generator& = delete;
54 private:
55 static const unsigned long seed = 19590921UL;
56
57 std::mt19937 uint_gen;
58 std::uniform_real_distribution<double> uniform_dist;
59 std::normal_distribution<double> normal_dist;
60
62 uint_gen(), uniform_dist(0.0, 1.0), normal_dist(0.0, 1.0)
63 { this->uint_gen.seed(seed); }
64
65 ~random_generator() = default;
66
67 public:
68 auto uniform() -> Scalar_T
69 { return Scalar_T(this->uniform_dist(this->uint_gen)); }
70 auto normal() -> Scalar_T
71 { return Scalar_T(this->normal_dist(this->uint_gen)); }
72 };
73}
74
75#endif // _GLUCAT_RANDOM_H
Random number generator with single instance per Scalar_T.
Definition: random.h:43
static auto generator() -> random_generator &
Single instance of Random number generator.
Definition: random.h:51
static const unsigned long seed
Definition: random.h:55
auto uniform() -> Scalar_T
Definition: random.h:68
std::normal_distribution< double > normal_dist
Definition: random.h:59
random_generator(const random_generator &)=delete
friend class friend_for_private_destructor
Definition: random.h:48
auto normal() -> Scalar_T
Definition: random.h:70
auto operator=(const random_generator &) -> random_generator &=delete
std::uniform_real_distribution< double > uniform_dist
Definition: random.h:58
std::mt19937 uint_gen
Definition: random.h:57