Provide more configurable random generator

parent d78cd81c
......@@ -69,12 +69,22 @@ namespace limbo {
class RandomGenerator {
public:
using result_type = typename D::result_type;
RandomGenerator(result_type a, result_type b) : _dist(a, b), _rgen(randutils::auto_seed_128{}.base()) {}
result_type rand()
RandomGenerator(result_type a, result_type b, int seed = -1) : _dist(a, b) { this->seed(seed); }
result_type rand() { return _dist(_rgen); }
void seed(int seed = -1)
{
return _dist(_rgen);
if (seed >= 0)
_rgen.seed(seed);
else
_rgen.seed(randutils::auto_seed_128{}.base());
}
void reset() { _dist.reset(); }
void param(const typename D::param_type& param) { _dist.param(param); }
private:
D _dist;
std::mt19937 _rgen;
......@@ -95,36 +105,41 @@ namespace limbo {
/// Double random number generator (gaussian)
using rgen_gauss_t = RandomGenerator<rdist_gauss_t>;
///@ingroup tools
///integer random number generator
/// @ingroup tools
/// integer random number generator
using rgen_int_t = RandomGenerator<rdist_int_t>;
/// @ingroup tools
/// random vector in [0, 1.0]
/// random vector by providing custom RandomGenerator
template <typename Rng>
inline Eigen::VectorXd random_vec(int size, Rng& rng)
{
Eigen::VectorXd res(size);
for (int i = 0; i < size; ++i)
res[i] = rng.rand();
return res;
}
/// @ingroup tools
/// random vector in [0, 1]
///
/// - this function is thread safe because we use a random generator for each thread
/// - we use a C++11 random number generator
inline Eigen::VectorXd random_vector_bounded(int size)
{
static thread_local rgen_double_t rgen(0.0, 1.0);
Eigen::VectorXd res(size);
for (int i = 0; i < size; ++i)
res[i] = rgen.rand();
return res;
return random_vec(size, rgen);
}
/// @ingroup tools
/// random vector generated with a normal distribution centered on 0, with standard deviation of 10.0
/// random vector generated with a normal distribution centered on 0, with standard deviation of 10
///
/// - this function is thread safe because we use a random generator for each thread
/// - we use a C++11 random number generator
inline Eigen::VectorXd random_vector_unbounded(int size)
{
static thread_local rgen_gauss_t rgen(0.0, 10.0);
Eigen::VectorXd res(size);
for (int i = 0; i < size; ++i)
res[i] = rgen.rand();
return res;
return random_vec(size, rgen);
}
/// @ingroup tools
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment