summaryrefslogtreecommitdiff
path: root/comphelper
diff options
context:
space:
mode:
authorCaolán McNamara <caolanm@redhat.com>2014-10-03 17:12:23 +0100
committerCaolán McNamara <caolanm@redhat.com>2014-10-06 14:13:27 +0100
commit8f436d3de7e99268a8862664d2cb2574231c3b18 (patch)
tree0a07dd3cecbd7bc5b0293006bb8691e33c5cca75 /comphelper
parente5ab3685550cf35c3eb9cb47530044f2d86433d5 (diff)
use comphelper::rng::uniform_*_distribution everywhere
and automatically seed from time on first use coverity#1242393 Don't call rand coverity#1242404 Don't call rand coverity#1242410 Don't call rand and additionally allow 0xFF as a value coverity#1242409 Don't call rand coverity#1242399 Don't call rand coverity#1242372 Don't call rand coverity#1242377 Don't call rand coverity#1242378 Don't call rand coverity#1242379 Don't call rand coverity#1242382 Don't call rand coverity#1242383 Don't call rand coverity#1242402 Don't call rand coverity#1242397 Don't call rand coverity#1242390 Don't call rand coverity#1242389 Don't call rand coverity#1242388 Don't call rand coverity#1242386 Don't call rand coverity#1242384 Don't call rand coverity#1242394 Don't call rand Change-Id: I241feab9cb370e091fd6ccaba2af941eb95bc7cf
Diffstat (limited to 'comphelper')
-rw-r--r--comphelper/source/misc/random.cxx58
1 files changed, 45 insertions, 13 deletions
diff --git a/comphelper/source/misc/random.cxx b/comphelper/source/misc/random.cxx
index a97608649bbf..4fb4a1fde08d 100644
--- a/comphelper/source/misc/random.cxx
+++ b/comphelper/source/misc/random.cxx
@@ -11,8 +11,8 @@
*/
#include <boost/random.hpp>
-
#include <comphelper/random.hxx>
+#include <rtl/instance.hxx>
// this is nothing but a simple wrapper around
// the boost random generators
@@ -29,23 +29,55 @@ namespace rng
// memory requirement: 625*sizeof(uint32_t)
// http://en.wikipedia.org/wiki/Mersenne_twister
#define BOOST_RNG_ALGO boost::mt19937
-BOOST_RNG_ALGO global_rng;
-// initialises the state of the global random number generator
-// should only be called once at the start of the main programme
-// (note, a few boost::variate_generator<> (like normal) have their
-// own state which would need a reset as well to guarantee identical
-// sequence of numbers, e.g. via myrand.distribution().reset())
-void seed(int i)
+struct RandomNumberGenerator
+{
+ BOOST_RNG_ALGO global_rng;
+ RandomNumberGenerator()
+ {
+ // initialises the state of the global random number generator
+ // should only be called once.
+ // (note, a few boost::variate_generator<> (like normal) have their
+ // own state which would need a reset as well to guarantee identical
+ // sequence of numbers, e.g. via myrand.distribution().reset())
+ global_rng.seed(time(NULL));
+ }
+};
+
+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)
+{
+ boost::random::uniform_int_distribution<int> dist(a, b);
+ return dist(theRandomNumberGenerator::get().global_rng);
+}
+
+// uniform ints [a,b] distribution
+unsigned int uniform_int_distribution(unsigned int a, unsigned int b)
+{
+ boost::random::uniform_int_distribution<unsigned int> dist(a, b);
+ return dist(theRandomNumberGenerator::get().global_rng);
+}
+
+// uniform size_t [a,b] distribution
+size_t uniform_int_distribution(size_t a, size_t b)
{
- global_rng.seed(i);
+ boost::random::uniform_int_distribution<size_t> dist(a, b);
+ return dist(theRandomNumberGenerator::get().global_rng);
}
-// uniform [0,1) or [a,b) distribution
-double uniform()
+// uniform size_t [a,b) distribution
+double uniform_real_distribution(double a, double b)
{
- static boost::uniform_01<BOOST_RNG_ALGO&> myrand(global_rng);
- return myrand();
+ boost::random::uniform_real_distribution<double> dist(a, b);
+ return dist(theRandomNumberGenerator::get().global_rng);
}
} // namespace