2012-12-06 14:05:11 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Copyright (C) 2012 Tino Kluge <tino.kluge@hrz.tu-chemnitz.de>
|
|
|
|
*/
|
|
|
|
|
2014-05-13 08:42:21 +02:00
|
|
|
#include <comphelper/random.hxx>
|
2014-10-03 17:12:23 +01:00
|
|
|
#include <rtl/instance.hxx>
|
2015-01-06 11:19:31 +00:00
|
|
|
#include <assert.h>
|
2015-01-06 15:33:18 +00:00
|
|
|
#include <time.h>
|
2015-01-06 11:19:31 +00:00
|
|
|
#include <random>
|
2014-04-14 15:41:13 +02:00
|
|
|
|
2012-12-06 14:05:11 +00:00
|
|
|
// this is nothing but a simple wrapper around
|
2015-01-06 11:19:31 +00:00
|
|
|
// the std::random generators
|
2012-12-06 14:05:11 +00:00
|
|
|
|
2014-05-13 08:42:21 +02:00
|
|
|
namespace comphelper
|
2012-12-06 14:05:11 +00:00
|
|
|
{
|
|
|
|
namespace rng
|
|
|
|
{
|
|
|
|
|
|
|
|
// underlying random number generator
|
2015-01-06 11:19:31 +00:00
|
|
|
// std::mt19937 implements the Mersenne twister algorithm which
|
2012-12-06 14:05:11 +00:00
|
|
|
// is fast and has good statistical properties, it produces integers
|
|
|
|
// in the range of [0, 2^32-1] internally
|
|
|
|
// memory requirement: 625*sizeof(uint32_t)
|
|
|
|
// http://en.wikipedia.org/wiki/Mersenne_twister
|
2015-01-06 11:19:31 +00:00
|
|
|
#define STD_RNG_ALGO std::mt19937
|
2012-12-06 14:05:11 +00:00
|
|
|
|
2014-10-03 17:12:23 +01:00
|
|
|
struct RandomNumberGenerator
|
|
|
|
{
|
2015-01-06 11:19:31 +00:00
|
|
|
STD_RNG_ALGO global_rng;
|
2014-10-03 17:12:23 +01:00
|
|
|
RandomNumberGenerator()
|
|
|
|
{
|
2015-05-01 22:58:26 -05:00
|
|
|
std::random_device rd;
|
2014-10-03 17:12:23 +01:00
|
|
|
// initialises the state of the global random number generator
|
|
|
|
// should only be called once.
|
2015-01-06 11:19:31 +00:00
|
|
|
// (note, a few std::variate_generator<> (like normal) have their
|
2014-10-03 17:12:23 +01:00
|
|
|
// own state which would need a reset as well to guarantee identical
|
|
|
|
// sequence of numbers, e.g. via myrand.distribution().reset())
|
2015-05-01 22:58:26 -05:00
|
|
|
global_rng.seed(rd() ^ time(nullptr));
|
2014-10-03 17:12:23 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class theRandomNumberGenerator : public rtl::Static<RandomNumberGenerator, theRandomNumberGenerator> {};
|
|
|
|
|
|
|
|
// re-initialises the state of the global random number generator
|
|
|
|
void reseed(int i)
|
|
|
|
{
|
|
|
|
return theRandomNumberGenerator::get().global_rng.seed(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// uniform ints [a,b] distribution
|
|
|
|
int uniform_int_distribution(int a, int b)
|
|
|
|
{
|
2015-01-06 11:19:31 +00:00
|
|
|
std::uniform_int_distribution<int> dist(a, b);
|
2014-10-03 17:12:23 +01:00
|
|
|
return dist(theRandomNumberGenerator::get().global_rng);
|
|
|
|
}
|
|
|
|
|
|
|
|
// uniform ints [a,b] distribution
|
2014-10-07 13:45:15 +01:00
|
|
|
unsigned int uniform_uint_distribution(unsigned int a, unsigned int b)
|
2014-10-03 17:12:23 +01:00
|
|
|
{
|
2015-01-06 11:19:31 +00:00
|
|
|
std::uniform_int_distribution<unsigned int> dist(a, b);
|
2014-10-03 17:12:23 +01:00
|
|
|
return dist(theRandomNumberGenerator::get().global_rng);
|
|
|
|
}
|
|
|
|
|
|
|
|
// uniform size_t [a,b] distribution
|
2014-10-07 13:45:15 +01:00
|
|
|
size_t uniform_size_distribution(size_t a, size_t b)
|
2012-12-06 14:05:11 +00:00
|
|
|
{
|
2015-01-06 11:19:31 +00:00
|
|
|
std::uniform_int_distribution<size_t> dist(a, b);
|
2014-10-03 17:12:23 +01:00
|
|
|
return dist(theRandomNumberGenerator::get().global_rng);
|
2012-12-06 14:05:11 +00:00
|
|
|
}
|
|
|
|
|
2014-10-03 17:12:23 +01:00
|
|
|
// uniform size_t [a,b) distribution
|
|
|
|
double uniform_real_distribution(double a, double b)
|
2012-12-06 14:05:11 +00:00
|
|
|
{
|
2014-10-07 10:32:52 +03:00
|
|
|
assert(a < b);
|
2015-01-06 11:19:31 +00:00
|
|
|
std::uniform_real_distribution<double> dist(a, b);
|
2014-10-03 17:12:23 +01:00
|
|
|
return dist(theRandomNumberGenerator::get().global_rng);
|
2012-12-06 14:05:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|