PaGMO  1.1.5
rng.h
1 /*****************************************************************************
2  * Copyright (C) 2004-2015 The PaGMO development team, *
3  * Advanced Concepts Team (ACT), European Space Agency (ESA) *
4  * *
5  * https://github.com/esa/pagmo *
6  * *
7  * act@esa.int *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the *
21  * Free Software Foundation, Inc., *
22  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23  *****************************************************************************/
24 
25 // 27/12/08 Created by Francesco Biscani.
26 
27 #ifndef PAGMO_RNG_H
28 #define PAGMO_RNG_H
29 
30 #include <boost/cstdint.hpp>
31 #include <boost/random/lagged_fibonacci.hpp>
32 #include <boost/random/mersenne_twister.hpp>
33 #include <boost/thread/locks.hpp>
34 #include <boost/thread/mutex.hpp>
35 #include <sstream>
36 #include <string>
37 
38 #include "serialization.h"
39 #include "config.h"
40 
41 namespace pagmo
42 {
44 
47 class __PAGMO_VISIBLE rng_uint32: public boost::mt19937 {
48  friend class boost::serialization::access;
49  public:
51  typedef boost::mt19937::result_type result_type;
53 
56  rng_uint32():boost::mt19937() {}
58 
61  rng_uint32(const result_type &n):boost::mt19937(n) {}
62  // Default generated copy ctor and assignment are fine.
63  private:
64  // Serialization exploits the fact that the state of Boost RNGs
65  // can be sent/received to/from standard streams.
66  template <class Archive>
67  void save(Archive &ar, const unsigned int) const
68  {
69  std::stringstream ss;
70  ss << *static_cast<boost::mt19937 const *>(this);
71  std::string tmp(ss.str());
72  ar << tmp;
73  }
74  template <class Archive>
75  void load(Archive &ar, const unsigned int)
76  {
77  std::string tmp;
78  ar >> tmp;
79  std::stringstream ss(tmp);
80  ss >> *static_cast<boost::mt19937 *>(this);
81  }
82  BOOST_SERIALIZATION_SPLIT_MEMBER()
83 };
84 
86 
89 class __PAGMO_VISIBLE rng_double: public boost::lagged_fibonacci607 {
90  friend class boost::serialization::access;
91  public:
93 
96  rng_double():boost::lagged_fibonacci607() {}
98 
101  rng_double(const boost::uint32_t &n):boost::lagged_fibonacci607(n) {}
102  // Default generated copy ctor and assignment are fine.
103  private:
104  template <class Archive>
105  void save(Archive &ar, const unsigned int) const
106  {
107  std::stringstream ss;
108  ss << *static_cast<boost::lagged_fibonacci607 const *>(this);
109  std::string tmp(ss.str());
110  ar << tmp;
111  }
112  template <class Archive>
113  void load(Archive &ar, const unsigned int)
114  {
115  std::string tmp;
116  ar >> tmp;
117  std::stringstream ss(tmp);
118  ss >> *static_cast<boost::lagged_fibonacci607 *>(this);
119  }
120  BOOST_SERIALIZATION_SPLIT_MEMBER()
121 };
122 
124 
138 class __PAGMO_VISIBLE rng_generator {
139  public:
141 
149  template <class Rng>
150  static Rng get();
151  static void set_seed(int);
152 
153  private:
154  static boost::mutex m_mutex;
155  static rng_uint32 m_seeder;
156 };
157 
158 }
159 
160 #endif
Root PaGMO namespace.
Generic thread-safe generator of pseudo-random number generators.
Definition: rng.h:138
This rng returns an unsigned integer in the [0,2**32-1] range.
Definition: rng.h:47
rng_double(const boost::uint32_t &n)
Constructor from unsigned integer.
Definition: rng.h:101
boost::mt19937::result_type result_type
Return value of the generator.
Definition: rng.h:51
rng_double()
Default constructor.
Definition: rng.h:96
rng_uint32()
Default constructor.
Definition: rng.h:56
rng_uint32(const result_type &n)
Constructor from unsigned integer.
Definition: rng.h:61
This rng returns a double in the [0,1[ range.
Definition: rng.h:89