All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rng_gsl.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2014, 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 #ifdef O2SCL_NEEDS_TIME_H
32 #include "time.h"
33 #endif
34 
35 #include <gsl/gsl_rng.h>
36 
37 #ifndef DOXYGEN_NO_O2NS
38 namespace o2scl {
39 #endif
40 
41  /** \brief Random number generator (GSL)
42 
43  If \c seed is zero, or is not given, then the default seed
44  specific to the particular random number generator is used.
45 
46  An interesting application of this class to generate an
47  arbitrary distribution through a Markov chain Monte Carlo
48  method is in \ref ex_markov_sect.
49  */
50 
51  class rng_gsl {
52 
53  public:
54 
55  /** \brief Initialize the random number generator with type \c gtype
56  and the default seed
57  */
58  rng_gsl(const gsl_rng_type *gtype=gsl_rng_mt19937);
59 
60  rng_gsl(double ig1, double ig2) {
61  rng=gsl_rng_mt19937;
62  gr=gsl_rng_alloc(gsl_rng_mt19937);
63  gsl_rng_set(gr,0);
64  seed=time(0);
65  }
66 
67  /// Initialize the random number generator with \c seed
68  rng_gsl(unsigned long int seed,
69  const gsl_rng_type *gtype=gsl_rng_mt19937);
70 
71  ~rng_gsl();
72 
73  /// Return generator type
74  const gsl_rng_type *get_type() { return rng; }
75 
76  /** \brief Return a random number in \f$(0,1]\f$
77  */
78  double operator()(int ignored) {
79  return random();
80  }
81 
82  /// Return a random number in \f$(0,1]\f$
83  double random() {
84  return (gr->type->get_double)(gr->state);
85  }
86 
87  /// Return the maximum integer for random_int()
88  unsigned long int get_max() {
89  return gsl_rng_max(gr)-gsl_rng_min(gr);
90  }
91 
92  /// Return random integer in \f$[0,\mathrm{max}-1]\f$.
93  unsigned long int random_int(unsigned long int n=0);
94 
95  /// Set the seed
96  void set_seed(unsigned long int s) {
97  seed=s;
98  gsl_rng_set(gr,seed);
99  }
100 
101  /// Set the seed
102  void clock_seed() {
103  seed=time(0);
104  gsl_rng_set(gr,seed);
105  }
106 
107 #ifndef DOXYGEN_INTERNAL
108 
109  private:
110 
111  rng_gsl(const rng_gsl &);
112  rng_gsl& operator=(const rng_gsl&);
113 
114  protected:
115 
116  /// The seed
117  unsigned long int seed;
118 
119  /// The GSL random number generator
120  gsl_rng *gr;
121 
122  /// The GSL random number generator type
123  const gsl_rng_type *rng;
124 
125 #endif
126 
127  };
128 
129 #ifndef DOXYGEN_NO_O2NS
130 }
131 #endif
132 
133 #endif
void clock_seed()
Set the seed.
Definition: rng_gsl.h:102
void set_seed(unsigned long int s)
Set the seed.
Definition: rng_gsl.h:96
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:83
unsigned long int seed
The seed.
Definition: rng_gsl.h:117
unsigned long int get_max()
Return the maximum integer for random_int()
Definition: rng_gsl.h:88
gsl_rng * gr
The GSL random number generator.
Definition: rng_gsl.h:120
Random number generator (GSL)
Definition: rng_gsl.h:51
unsigned long int random_int(unsigned long int n=0)
Return random integer in .
const gsl_rng_type * get_type()
Return generator type.
Definition: rng_gsl.h:74
const gsl_rng_type * rng
The GSL random number generator type.
Definition: rng_gsl.h:123
double operator()(int ignored)
Return a random number in .
Definition: rng_gsl.h:78

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).
Hosted at Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads..