rng_gsl.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2017, 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  /// Set the seed
112  void clock_seed() {
113  seed=time(0);
114  gsl_rng_set(this,seed);
115  }
116 
117  /// Copy constructor with equals operator
118  rng_gsl& operator=(const rng_gsl &rg) {
119  if (this!=&rg) {
120  seed=rg.seed;
121  rng=rg.rng;
122  this->state=malloc(rg.type->size);
123  this->type=rg.type;
124  memcpy(this->state,rg.state,this->type->size);
125  }
126  return *this;
127  }
128 
129  /// Copy constructor
130  rng_gsl(const rng_gsl &rg) {
131  seed=rg.seed;
132  rng=rg.rng;
133  this->state=malloc(rg.type->size);
134  this->type=rg.type;
135  memcpy(this->state,rg.state,this->type->size);
136  }
137 
138 #ifndef DOXYGEN_INTERNAL
139 
140  protected:
141 
142  /// The seed
143  unsigned long int seed;
144 
145  /// The GSL random number generator type
146  const gsl_rng_type *rng;
147 
148 #endif
149 
150  };
151 
152  /** \brief Desc
153  */
155  public:
156  double operator()(rng_gsl &r) {
157  return (r.type->get_double)(r.state);
158  }
159  };
160 
161 #ifndef DOXYGEN_NO_O2NS
162 }
163 #endif
164 
165 #endif
void clock_seed()
Set the seed.
Definition: rng_gsl.h:112
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
unsigned long int seed
The seed.
Definition: rng_gsl.h:143
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
rng_gsl & operator=(const rng_gsl &rg)
Copy constructor with equals operator.
Definition: rng_gsl.h:118
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:130
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:146

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