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 // underlying random number generator
31 // std::mt19937 implements the Mersenne twister algorithm which
32 // is fast and has good statistical properties, it produces integers
33 // in the range of [0, 2^32-1] internally
34 // memory requirement: 625*sizeof(uint32_t)
35 // http://en.wikipedia.org/wiki/Mersenne_twister
36 #define STD_RNG_ALGO std::mt19937
37 
38 namespace
39 {
40 struct RandomNumberGenerator
41 {
44  RandomNumberGenerator()
45  {
46  bool bRepeatable = (getenv("SAL_RAND_REPEATABLE") != 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)
51  bRepeatable = true;
52 #endif
53  if (bRepeatable)
54  {
55  global_rng.seed(42);
56  return;
57  }
58 
59  try
60  {
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));
68  }
69  catch (std::runtime_error& e)
70  {
71  SAL_WARN("comphelper", "Using std::random_device failed: " << e.what());
72  global_rng.seed(time(nullptr));
73  }
74  }
75 };
76 
77 class theRandomNumberGenerator : public rtl::Static<RandomNumberGenerator, theRandomNumberGenerator>
78 {
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:36
double uniform_real_distribution(double a, double b)
uniform distribution in [a,b)
Definition: random.cxx:110
std::mutex mutex
Definition: random.cxx:42
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:43