PaGMO  1.1.5
rng.cpp
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 #include <boost/date_time/posix_time/posix_time.hpp>
26 #include <boost/thread/locks.hpp>
27 #include <boost/thread/mutex.hpp>
28 
29 #include "rng.h"
30 
31 namespace pagmo
32 {
33 
34 boost::mutex rng_generator::m_mutex;
35 
36 // Use as initial seed the number of microseconds elapsed since 01/01/1970, cast to uint32_t.
37 rng_uint32 rng_generator::m_seeder(boost::uint32_t((boost::posix_time::microsec_clock::local_time() -
38  boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds()));
39 
41 
48 {
49  boost::lock_guard<boost::mutex> lock(m_mutex);
50  m_seeder.seed(n);
51 }
52 
53 
54 template <class Rng>
56 {
57  boost::lock_guard<boost::mutex> lock(m_mutex);
58  return Rng(m_seeder());
59 }
60 
61 template __PAGMO_VISIBLE rng_double rng_generator::get<rng_double>();
62 template __PAGMO_VISIBLE rng_uint32 rng_generator::get<rng_uint32>();
63 
64 }
Root PaGMO namespace.
This rng returns an unsigned integer in the [0,2**32-1] range.
Definition: rng.h:47
static void set_seed(int)
Set seed.
Definition: rng.cpp:47
static Rng get()
Return pseudo-random number generator.
Definition: rng.cpp:55
This rng returns a double in the [0,1[ range.
Definition: rng.h:89