LibreOffice Module comphelper (master)  1
random.cxx
Go to the documentation of this file.
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 <comphelper/random.hxx>
14 #include <rtl/instance.hxx>
15 #include <sal/log.hxx>
16 #include <assert.h>
17 #include <time.h>
18 #include <mutex>
19 #include <random>
20 #include <stdexcept>
21 #if defined HAVE_VALGRIND_HEADERS
22 #include <valgrind/memcheck.h>
23 #endif
24 
25 // this is nothing but a simple wrapper around
26 // the std::random generators
27 
28 namespace comphelper::rng
29 {
30 
31 // underlying random number generator
32 // std::mt19937 implements the Mersenne twister algorithm which
33 // is fast and has good statistical properties, it produces integers
34 // in the range of [0, 2^32-1] internally
35 // memory requirement: 625*sizeof(uint32_t)
36 // http://en.wikipedia.org/wiki/Mersenne_twister
37 #define STD_RNG_ALGO std::mt19937
38 
39 namespace {
40 
41 struct RandomNumberGenerator
42 {
45  RandomNumberGenerator()
46  {
47  bool bRepeatable = (getenv("SAL_RAND_REPEATABLE") != nullptr);
48  // valgrind on some platforms (e.g.Ubuntu16.04) does not support the new Intel RDRAND instructions,
49  // which leads to "Illegal Opcode" errors, so just turn off randomness.
50 #if defined HAVE_VALGRIND_HEADERS
51  if (RUNNING_ON_VALGRIND)
52  bRepeatable = true;
53 #endif
54  if (bRepeatable)
55  {
56  global_rng.seed(42);
57  return;
58  }
59 
60  try
61  {
62  std::random_device rd;
63  // initialises the state of the global random number generator
64  // should only be called once.
65  // (note, a few std::variate_generator<> (like normal) have their
66  // own state which would need a reset as well to guarantee identical
67  // sequence of numbers, e.g. via myrand.distribution().reset())
68  global_rng.seed(rd() ^ time(nullptr));
69  }
70  catch (std::runtime_error& e)
71  {
72  SAL_WARN("comphelper", "Using std::random_device failed: " << e.what());
73  global_rng.seed(time(nullptr));
74  }
75  }
76 };
77 
78 class theRandomNumberGenerator : public rtl::Static<RandomNumberGenerator, theRandomNumberGenerator> {};
79 
80 }
81 
82 // uniform ints [a,b] distribution
83 int uniform_int_distribution(int a, int b)
84 {
85  std::uniform_int_distribution<int> dist(a, b);
86  auto & gen = theRandomNumberGenerator::get();
87  std::scoped_lock<std::mutex> g(gen.mutex);
88  return dist(gen.global_rng);
89 }
90 
91 // uniform ints [a,b] distribution
92 unsigned int uniform_uint_distribution(unsigned int a, unsigned int b)
93 {
94  std::uniform_int_distribution<unsigned int> dist(a, b);
95  auto & gen = theRandomNumberGenerator::get();
96  std::scoped_lock<std::mutex> g(gen.mutex);
97  return dist(gen.global_rng);
98 }
99 
100 // uniform size_t [a,b] distribution
101 size_t uniform_size_distribution(size_t a, size_t b)
102 {
103  std::uniform_int_distribution<size_t> dist(a, b);
104  auto & gen = theRandomNumberGenerator::get();
105  std::scoped_lock<std::mutex> g(gen.mutex);
106  return dist(gen.global_rng);
107 }
108 
109 // uniform size_t [a,b) distribution
110 double uniform_real_distribution(double a, double b)
111 {
112  assert(a < b);
113  std::uniform_real_distribution<double> dist(a, b);
114  auto & gen = theRandomNumberGenerator::get();
115  std::scoped_lock<std::mutex> g(gen.mutex);
116  return dist(gen.global_rng);
117 }
118 
119 } // namespace
120 
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
unsigned int uniform_uint_distribution(unsigned int a, unsigned int b)
uniform distribution in [a,b]
Definition: random.cxx:92
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
size_t uniform_size_distribution(size_t a, size_t b)
uniform distribution in [a,b]
Definition: random.cxx:101
#define STD_RNG_ALGO
Definition: random.cxx:37
double uniform_real_distribution(double a, double b)
uniform distribution in [a,b)
Definition: random.cxx:110
std::mutex mutex
Definition: random.cxx:43
int uniform_int_distribution(int a, int b)
uniform distribution in [a,b]
Definition: random.cxx:83
#define SAL_WARN(area, stream)
STD_RNG_ALGO global_rng
Definition: random.cxx:44