bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / misc / random.cxx
blobd8e81c5175a960644cf62adb135e4fe11d25e3da
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * Contributor(s):
10 * Copyright (C) 2012 Tino Kluge <tino.kluge@hrz.tu-chemnitz.de>
13 #include <comphelper/random.hxx>
14 #include <rtl/instance.hxx>
15 #include <sal/log.hxx>
16 #include <assert.h>
17 #include <time.h>
18 #include <random>
19 #include <stdexcept>
21 // this is nothing but a simple wrapper around
22 // the std::random generators
24 namespace comphelper
26 namespace rng
29 // underlying random number generator
30 // std::mt19937 implements the Mersenne twister algorithm which
31 // is fast and has good statistical properties, it produces integers
32 // in the range of [0, 2^32-1] internally
33 // memory requirement: 625*sizeof(uint32_t)
34 // http://en.wikipedia.org/wiki/Mersenne_twister
35 #define STD_RNG_ALGO std::mt19937
37 struct RandomNumberGenerator
39 STD_RNG_ALGO global_rng;
40 RandomNumberGenerator()
42 try
44 std::random_device rd;
45 // initialises the state of the global random number generator
46 // should only be called once.
47 // (note, a few std::variate_generator<> (like normal) have their
48 // own state which would need a reset as well to guarantee identical
49 // sequence of numbers, e.g. via myrand.distribution().reset())
50 global_rng.seed(rd() ^ time(nullptr));
52 catch (std::runtime_error& e)
54 SAL_WARN("comphelper.random", "Using std::random_device failed: " << e.what());
55 global_rng.seed(time(nullptr));
60 class theRandomNumberGenerator : public rtl::Static<RandomNumberGenerator, theRandomNumberGenerator> {};
62 // re-initialises the state of the global random number generator
63 void reseed(int i)
65 return theRandomNumberGenerator::get().global_rng.seed(i);
68 // uniform ints [a,b] distribution
69 int uniform_int_distribution(int a, int b)
71 std::uniform_int_distribution<int> dist(a, b);
72 return dist(theRandomNumberGenerator::get().global_rng);
75 // uniform ints [a,b] distribution
76 unsigned int uniform_uint_distribution(unsigned int a, unsigned int b)
78 std::uniform_int_distribution<unsigned int> dist(a, b);
79 return dist(theRandomNumberGenerator::get().global_rng);
82 // uniform size_t [a,b] distribution
83 size_t uniform_size_distribution(size_t a, size_t b)
85 std::uniform_int_distribution<size_t> dist(a, b);
86 return dist(theRandomNumberGenerator::get().global_rng);
89 // uniform size_t [a,b) distribution
90 double uniform_real_distribution(double a, double b)
92 assert(a < b);
93 std::uniform_real_distribution<double> dist(a, b);
94 return dist(theRandomNumberGenerator::get().global_rng);
97 } // namespace
98 } // namespace
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */