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 <sal/log.hxx>
15 #include <assert.h>
16 #include <time.h>
17 #include <mutex>
18 #include <random>
19 #include <stdexcept>
20 #if defined HAVE_VALGRIND_HEADERS
21 #include <valgrind/memcheck.h>
22 #endif
23 
24 // this is nothing but a simple wrapper around
25 // the std::random generators
26 
27 namespace comphelper::rng
28 {
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
36 
37 namespace
38 {
39 struct RandomNumberGenerator
40 {
43  RandomNumberGenerator()
44  {
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)
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 RandomNumberGenerator& GetTheRandomNumberGenerator()
78 {
79  static RandomNumberGenerator RANDOM;
80  return RANDOM;
81 }
82 }
83 
84 // uniform ints [a,b] distribution
85 int uniform_int_distribution(int a, int b)
86 {
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);
91 }
92 
93 // uniform ints [a,b] distribution
94 unsigned int uniform_uint_distribution(unsigned int a, unsigned int b)
95 {
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);
100 }
101 
102 // uniform size_t [a,b] distribution
103 size_t uniform_size_distribution(size_t a, size_t b)
104 {
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);
109 }
110 
111 // uniform size_t [a,b) distribution
112 double uniform_real_distribution(double a, double b)
113 {
114  assert(a < 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);
119 }
120 
121 } // namespace
122 
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
RANDOM
unsigned int uniform_uint_distribution(unsigned int a, unsigned int b)
uniform distribution in [a,b]
Definition: random.cxx:94
size_t uniform_size_distribution(size_t a, size_t b)
uniform distribution in [a,b]
Definition: random.cxx:103
#define STD_RNG_ALGO
Definition: random.cxx:35
double uniform_real_distribution(double a, double b)
uniform distribution in [a,b)
Definition: random.cxx:112
std::mutex mutex
Definition: random.cxx:41
int uniform_int_distribution(int a, int b)
uniform distribution in [a,b]
Definition: random.cxx:85
#define SAL_WARN(area, stream)
STD_RNG_ALGO global_rng
Definition: random.cxx:42