2013-07-17 19:24:07 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
*
|
|
|
|
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
|
|
|
|
*
|
2014-05-29 21:35:00 +02:00
|
|
|
* Copyright (C) 2011-2014 by M. Ferrero, O. Parcollet
|
2013-07-17 19:24:07 +02:00
|
|
|
*
|
|
|
|
* TRIQS is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* TRIQS. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
2014-05-29 21:35:00 +02:00
|
|
|
#pragma once
|
2014-01-16 21:17:13 +01:00
|
|
|
#include <triqs/utility/first_include.hpp>
|
2013-07-17 19:24:07 +02:00
|
|
|
#include "../utility/exceptions.hpp"
|
2014-05-29 21:35:00 +02:00
|
|
|
#include "../utility/buffered_function.hpp"
|
2013-07-17 19:24:07 +02:00
|
|
|
#include "math.h"
|
|
|
|
#include <string>
|
2014-05-29 21:35:00 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace triqs {
|
|
|
|
namespace mc_tools {
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2014-05-29 21:35:00 +02:00
|
|
|
/// Return a list of the names of available generators, with separator sep
|
|
|
|
std::string random_generator_names(std::string const & sep=" ");
|
|
|
|
std::vector<std::string> random_generator_names_list();
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
/**
|
2014-05-29 21:35:00 +02:00
|
|
|
* Random generator, adapting the boost random generator.
|
|
|
|
*
|
|
|
|
* The name of the generator is given at construction, and its type is erased in this class.
|
|
|
|
* For performance, the call to the generator is bufferized, with chunks of 1000 numbers.
|
2013-07-17 19:24:07 +02:00
|
|
|
*/
|
|
|
|
class random_generator {
|
2014-05-29 21:35:00 +02:00
|
|
|
utility::buffered_function<double> gen;
|
|
|
|
std::string _name;
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2014-05-29 21:35:00 +02:00
|
|
|
public:
|
|
|
|
/** Constructor
|
|
|
|
* @param RandomGeneratorName : Name of a boost generator e.g. mt19937, or "" (another Mersenne Twister).
|
|
|
|
* @param seed : The seed of the random generator
|
|
|
|
*/
|
|
|
|
random_generator(std::string const& RandomGeneratorName, uint32_t seed_);
|
|
|
|
|
|
|
|
random_generator() : random_generator("mt19937", 198) {}
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
///
|
2014-05-29 21:35:00 +02:00
|
|
|
random_generator(random_generator const& p);
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2014-05-29 21:35:00 +02:00
|
|
|
random_generator(random_generator&&) = default;
|
|
|
|
|
|
|
|
//
|
|
|
|
random_generator & operator=(random_generator&&) = default;
|
|
|
|
|
|
|
|
/// Name of the random generator
|
|
|
|
std::string name() const { return _name; }
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
/// Returns a integer in [0,i-1] with flat distribution
|
2014-05-29 21:35:00 +02:00
|
|
|
template <typename T> typename std::enable_if<std::is_integral<T>::value, T>::type operator()(T i) {
|
|
|
|
return (i == 1 ? 0 : T(floor(i * (gen()))));
|
|
|
|
}
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2014-04-02 20:36:54 +02:00
|
|
|
/// Returns a double in [0,1[ with flat distribution
|
2014-05-29 21:35:00 +02:00
|
|
|
double preview() { return gen.preview();}
|
2014-04-02 20:36:54 +02:00
|
|
|
|
2014-05-29 21:35:00 +02:00
|
|
|
double operator()() { return gen(); }
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
/// Returns a double in [0,x[ with flat distribution
|
2014-05-29 21:35:00 +02:00
|
|
|
double operator()(double x) { return x * (gen()); }
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
/// Returns a double in [a,b[ with flat distribution
|
2014-05-29 21:35:00 +02:00
|
|
|
double operator()(double a, double b) {
|
|
|
|
assert(b > a);
|
|
|
|
return a + (b - a) * (gen());
|
|
|
|
}
|
2013-07-17 19:24:07 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|