Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * Contributor(s):
10 : * Copyright (C) 2012 Tino Kluge <tino.kluge@hrz.tu-chemnitz.de>
11 : */
12 :
13 : #include <boost/random.hpp>
14 : #include <comphelper/random.hxx>
15 : #include <rtl/instance.hxx>
16 :
17 : // this is nothing but a simple wrapper around
18 : // the boost random generators
19 :
20 : namespace comphelper
21 : {
22 : namespace rng
23 : {
24 :
25 : // underlying random number generator
26 : // boost::mt19937 implements the Mersenne twister algorithm which
27 : // is fast and has good statistical properties, it produces integers
28 : // in the range of [0, 2^32-1] internally
29 : // memory requirement: 625*sizeof(uint32_t)
30 : // http://en.wikipedia.org/wiki/Mersenne_twister
31 : #define BOOST_RNG_ALGO boost::mt19937
32 :
33 : struct RandomNumberGenerator
34 : {
35 : BOOST_RNG_ALGO global_rng;
36 200 : RandomNumberGenerator()
37 200 : {
38 : // initialises the state of the global random number generator
39 : // should only be called once.
40 : // (note, a few boost::variate_generator<> (like normal) have their
41 : // own state which would need a reset as well to guarantee identical
42 : // sequence of numbers, e.g. via myrand.distribution().reset())
43 200 : global_rng.seed(time(NULL));
44 200 : }
45 : };
46 :
47 : class theRandomNumberGenerator : public rtl::Static<RandomNumberGenerator, theRandomNumberGenerator> {};
48 :
49 : // re-initialises the state of the global random number generator
50 0 : void reseed(int i)
51 : {
52 0 : return theRandomNumberGenerator::get().global_rng.seed(i);
53 : }
54 :
55 : // uniform ints [a,b] distribution
56 76339 : int uniform_int_distribution(int a, int b)
57 : {
58 76339 : boost::random::uniform_int_distribution<int> dist(a, b);
59 76339 : return dist(theRandomNumberGenerator::get().global_rng);
60 : }
61 :
62 : // uniform ints [a,b] distribution
63 0 : unsigned int uniform_uint_distribution(unsigned int a, unsigned int b)
64 : {
65 0 : boost::random::uniform_int_distribution<unsigned int> dist(a, b);
66 0 : return dist(theRandomNumberGenerator::get().global_rng);
67 : }
68 :
69 : // uniform size_t [a,b] distribution
70 40764 : size_t uniform_size_distribution(size_t a, size_t b)
71 : {
72 40764 : boost::random::uniform_int_distribution<size_t> dist(a, b);
73 40764 : return dist(theRandomNumberGenerator::get().global_rng);
74 : }
75 :
76 : // uniform size_t [a,b) distribution
77 0 : double uniform_real_distribution(double a, double b)
78 : {
79 : assert(a < b);
80 0 : boost::random::uniform_real_distribution<double> dist(a, b);
81 0 : return dist(theRandomNumberGenerator::get().global_rng);
82 : }
83 :
84 : } // namespace
85 3315 : } // namespace
86 :
87 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|