rng_gsl.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2018, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 /** \file rng_gsl.h
24  \brief File for definition of \ref o2scl::rng_gsl
25 */
26 #ifndef O2SCL_RNG_GSL_H
27 #define O2SCL_RNG_GSL_H
28 
29 #include <iostream>
30 
31 // For memcpy()
32 #include <cstring>
33 
34 #ifdef O2SCL_NEEDS_TIME_H
35 #include "time.h"
36 #endif
37 
38 #include <gsl/gsl_rng.h>
39 
40 #ifndef DOXYGEN_NO_O2NS
41 namespace o2scl {
42 #endif
43 
44  /** \brief Random number generator (GSL)
45 
46  This object is built on the <tt>gsl_rng</tt> struct and modeled
47  to look like a <tt>std::random_device</tt> object.
48 
49  If \c seed is zero, or is not given, then the default seed
50  specific to the particular random number generator is used.
51 
52  \future This will likely be completely replaced by the random
53  number generators in the standard library.
54  */
55  class rng_gsl : public gsl_rng {
56 
57  public:
58 
59  typedef unsigned long int result_type;
60 
61  /** \brief Initialize the random number generator with type \c gtype
62  and the default seed
63  */
64  rng_gsl(const gsl_rng_type *gtype=gsl_rng_mt19937);
65 
66  /// Initialize the random number generator with \c seed
67  rng_gsl(unsigned long int seed,
68  const gsl_rng_type *gtype=gsl_rng_mt19937);
69 
70  ~rng_gsl();
71 
72  /// Return generator type
73  const gsl_rng_type *get_type() { return rng; }
74 
75  /** \brief Return a random number in \f$(0,1]\f$
76  */
77  result_type operator()() {
78  return gsl_rng_get(this);
79  }
80 
81  /// Return a random number in \f$(0,1]\f$
82  double random() {
83  return (this->type->get_double)(this->state);
84  }
85 
86  /** \brief Return the entropy (0.0 since not applicable for
87  pseudo-random engines */
88  double entropy() {
89  return 0.0;
90  }
91 
92  /// Return the maximum integer for random_int()
93  unsigned long int max() {
94  return gsl_rng_max(this);
95  }
96 
97  /// Return the minimum integer for random_int()
98  unsigned long int min() {
99  return gsl_rng_min(this);
100  }
101 
102  /// Return random integer in \f$[0,\mathrm{max}-1]\f$.
103  unsigned long int random_int(unsigned long int n=0);
104 
105  /// Set the seed
106  void set_seed(unsigned long int s) {
107  seed=s;
108  gsl_rng_set(this,seed);
109  }
110 
111  /// Get the seed
112  unsigned long int get_seed() {
113  return seed;
114  }
115 
116  /// Set the seed
117  void clock_seed() {
118  seed=time(0);
119  gsl_rng_set(this,seed);
120  }
121 
122  /// Copy constructor with equals operator
123  rng_gsl& operator=(const rng_gsl &rg) {
124  if (this!=&rg) {
125  seed=rg.seed;
126  rng=rg.rng;
127  this->state=malloc(rg.type->size);
128  this->type=rg.type;
129  memcpy(this->state,rg.state,this->type->size);
130  }
131  return *this;
132  }
133 
134  /// Copy constructor
135  rng_gsl(const rng_gsl &rg) {
136  seed=rg.seed;
137  rng=rg.rng;
138  this->state=malloc(rg.type->size);
139  this->type=rg.type;
140  memcpy(this->state,rg.state,this->type->size);
141  }
142 
143 #ifndef DOXYGEN_INTERNAL
144 
145  protected:
146 
147  /// The seed
148  unsigned long int seed;
149 
150  /// The GSL random number generator type
151  const gsl_rng_type *rng;
152 
153 #endif
154 
155  };
156 
157  /** \brief An alternative RNG type used to design templates
158  for either GSL or STL random number generators
159  */
161  public:
162  double operator()(rng_gsl &r) {
163  return (r.type->get_double)(r.state);
164  }
165  };
166 
167 #ifndef DOXYGEN_NO_O2NS
168 }
169 #endif
170 
171 #endif
void clock_seed()
Set the seed.
Definition: rng_gsl.h:117
The main O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$scl names...
Definition: anneal.h:42
void set_seed(unsigned long int s)
Set the seed.
Definition: rng_gsl.h:106
rng_gsl(const gsl_rng_type *gtype=gsl_rng_mt19937)
Initialize the random number generator with type gtype and the default seed.
double random()
Return a random number in .
Definition: rng_gsl.h:82
An alternative RNG type used to design templates for either GSL or STL random number generators...
Definition: rng_gsl.h:160
unsigned long int seed
The seed.
Definition: rng_gsl.h:148
double entropy()
Return the entropy (0.0 since not applicable for pseudo-random engines.
Definition: rng_gsl.h:88
unsigned long int max()
Return the maximum integer for random_int()
Definition: rng_gsl.h:93
result_type operator()()
Return a random number in .
Definition: rng_gsl.h:77
unsigned long int get_seed()
Get the seed.
Definition: rng_gsl.h:112
rng_gsl & operator=(const rng_gsl &rg)
Copy constructor with equals operator.
Definition: rng_gsl.h:123
Random number generator (GSL)
Definition: rng_gsl.h:55
unsigned long int random_int(unsigned long int n=0)
Return random integer in .
unsigned long int min()
Return the minimum integer for random_int()
Definition: rng_gsl.h:98
rng_gsl(const rng_gsl &rg)
Copy constructor.
Definition: rng_gsl.h:135
const gsl_rng_type * get_type()
Return generator type.
Definition: rng_gsl.h:73
const gsl_rng_type * rng
The GSL random number generator type.
Definition: rng_gsl.h:151

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).