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  bool bRepeatable = (getenv("SAL_RAND_REPEATABLE") != nullptr);
46  // valgrind on some platforms (e.g.Ubuntu16.04) does not support the new Intel RDRAND instructions,
47  // which leads to "Illegal Opcode" errors, so just turn off randomness.
48 #if defined HAVE_VALGRIND_HEADERS
49  if (RUNNING_ON_VALGRIND)
50  bRepeatable = true;
51 #endif
52  if (bRepeatable)
53  {
54  global_rng.seed(42);
55  return;
56  }
57 
58  try
59  {
60  std::random_device rd;
61  // initialises the state of the global random number generator
62  // should only be called once.
63  // (note, a few std::variate_generator<> (like normal) have their
64  // own state which would need a reset as well to guarantee identical
65  // sequence of numbers, e.g. via myrand.distribution().reset())
66  global_rng.seed(rd() ^ time(nullptr));
67  }
68  catch (std::runtime_error& e)
69  {
70  SAL_WARN("comphelper", "Using std::random_device failed: " << e.what());
71  global_rng.seed(time(nullptr));
72  }
73  }
74 };
75 
76 RandomNumberGenerator& GetTheRandomNumberGenerator()
77 {
78  static RandomNumberGenerator RANDOM;
79  return RANDOM;
80 }
81 }
82 
83 // uniform ints [a,b] distribution
84 int uniform_int_distribution(int a, int b)
85 {
86  std::uniform_int_distribution<int> dist(a, b);
87  auto& gen = GetTheRandomNumberGenerator();
88  std::scoped_lock<std::mutex> g(gen.mutex);
89  return dist(gen.global_rng);
90 }
91 
92 // uniform ints [a,b] distribution
93 unsigned int uniform_uint_distribution(unsigned int a, unsigned int b)
94 {
95  std::uniform_int_distribution<unsigned int> dist(a, b);
96  auto& gen = GetTheRandomNumberGenerator();
97  std::scoped_lock<std::mutex> g(gen.mutex);
98  return dist(gen.global_rng);
99 }
100 
101 // uniform size_t [a,b] distribution
102 size_t uniform_size_distribution(size_t a, size_t b)
103 {
104  std::uniform_int_distribution<size_t> dist(a, b);
105  auto& gen = GetTheRandomNumberGenerator();
106  std::scoped_lock<std::mutex> g(gen.mutex);
107  return dist(gen.global_rng);
108 }
109 
110 // uniform size_t [a,b) distribution
111 double uniform_real_distribution(double a, double b)
112 {
113  assert(a < b);
114  std::uniform_real_distribution<double> dist(a, b);
115  auto& gen = GetTheRandomNumberGenerator();
116  std::scoped_lock<std::mutex> g(gen.mutex);
117  return dist(gen.global_rng);
118 }
119 
120 } // namespace
121 
122 /* 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:93
size_t uniform_size_distribution(size_t a, size_t b)
uniform distribution in [a,b]
Definition: random.cxx:102
#define STD_RNG_ALGO
Definition: random.cxx:35
double uniform_real_distribution(double a, double b)
uniform distribution in [a,b)
Definition: random.cxx:111
std::mutex mutex
Definition: random.cxx:41
int uniform_int_distribution(int a, int b)
uniform distribution in [a,b]
Definition: random.cxx:84
#define SAL_WARN(area, stream)
STD_RNG_ALGO global_rng
Definition: random.cxx:42