1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 * Copyright (C) 2012 Tino Kluge <tino.kluge@hrz.tu-chemnitz.de>
13 #include <comphelper/random.hxx>
14 #include <sal/log.hxx>
20 #if defined HAVE_VALGRIND_HEADERS
21 #include <valgrind/memcheck.h>
24 // this is nothing but a simple wrapper around
25 // the std::random generators
27 namespace comphelper::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
39 struct RandomNumberGenerator
42 STD_RNG_ALGO global_rng
;
43 RandomNumberGenerator()
45 // make RR easier to use, breaks easily without the RNG being repeatable
46 bool bRepeatable
= (getenv("SAL_RAND_REPEATABLE") != nullptr) || (getenv("RR") != nullptr);
47 // valgrind on some platforms (e.g.Ubuntu16.04) does not support the new Intel RDRAND instructions,
48 // which leads to "Illegal Opcode" errors, so just turn off randomness.
49 #if defined HAVE_VALGRIND_HEADERS
50 if (RUNNING_ON_VALGRIND
)
61 std::random_device rd
;
62 // initialises the state of the global random number generator
63 // should only be called once.
64 // (note, a few std::variate_generator<> (like normal) have their
65 // own state which would need a reset as well to guarantee identical
66 // sequence of numbers, e.g. via myrand.distribution().reset())
67 global_rng
.seed(rd() ^ time(nullptr));
69 catch (std::runtime_error
& e
)
71 SAL_WARN("comphelper", "Using std::random_device failed: " << e
.what());
72 global_rng
.seed(time(nullptr));
77 RandomNumberGenerator
& GetTheRandomNumberGenerator()
79 static RandomNumberGenerator RANDOM
;
84 // uniform ints [a,b] distribution
85 int uniform_int_distribution(int a
, int b
)
87 std::uniform_int_distribution
<int> dist(a
, b
);
88 auto& gen
= GetTheRandomNumberGenerator();
89 std::scoped_lock
<std::mutex
> g(gen
.mutex
);
90 return dist(gen
.global_rng
);
93 // uniform ints [a,b] distribution
94 unsigned int uniform_uint_distribution(unsigned int a
, unsigned int b
)
96 std::uniform_int_distribution
<unsigned int> dist(a
, b
);
97 auto& gen
= GetTheRandomNumberGenerator();
98 std::scoped_lock
<std::mutex
> g(gen
.mutex
);
99 return dist(gen
.global_rng
);
102 // uniform size_t [a,b] distribution
103 size_t uniform_size_distribution(size_t a
, size_t b
)
105 std::uniform_int_distribution
<size_t> dist(a
, b
);
106 auto& gen
= GetTheRandomNumberGenerator();
107 std::scoped_lock
<std::mutex
> g(gen
.mutex
);
108 return dist(gen
.global_rng
);
111 // uniform size_t [a,b) distribution
112 double uniform_real_distribution(double a
, double b
)
115 std::uniform_real_distribution
<double> dist(a
, b
);
116 auto& gen
= GetTheRandomNumberGenerator();
117 std::scoped_lock
<std::mutex
> g(gen
.mutex
);
118 return dist(gen
.global_rng
);
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */