0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Random.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 #include <Common/Compat.h>
27 
28 #include "Random.h"
29 
30 #include <cassert>
31 #include <mutex>
32 #include <random>
33 
34 #if defined(__APPLE__)
35 #define THREAD_LOCAL
36 #define LOCK_GLOBAL_MUTEX(m) std::lock_guard<std::mutex> lock(m)
37 #else
38 #define THREAD_LOCAL thread_local
39 #define LOCK_GLOBAL_MUTEX(m) (void)m
40 #endif
41 
42 using namespace Hypertable;
43 using namespace std;
44 
45 namespace {
46  THREAD_LOCAL mt19937 g_random_engine {1};
47  std::mutex g_mutex;
48 }
49 
50 void Random::seed(unsigned int s) {
51  LOCK_GLOBAL_MUTEX(g_mutex);
52  g_random_engine.seed(s);
53 }
54 
55 uint32_t Random::number32(uint32_t maximum) {
56  LOCK_GLOBAL_MUTEX(g_mutex);
57  if (maximum) {
58  return uniform_int_distribution<uint32_t>(0, maximum-1)(g_random_engine);
59  }
60  return uniform_int_distribution<uint32_t>()(g_random_engine);
61 }
62 
63 int64_t Random::number64(int64_t maximum) {
64  LOCK_GLOBAL_MUTEX(g_mutex);
65  if (maximum) {
66  assert(maximum > 0);
67  return uniform_int_distribution<int64_t>(0, maximum-1)(g_random_engine);
68  }
69  return uniform_int_distribution<int64_t>()(g_random_engine);
70 }
71 
73  LOCK_GLOBAL_MUTEX(g_mutex);
74  return uniform_real_distribution<>()(g_random_engine);
75 }
76 
77 chrono::milliseconds Random::duration_millis(uint32_t maximum) {
78  LOCK_GLOBAL_MUTEX(g_mutex);
79  assert(maximum > 0);
80  uniform_int_distribution<uint32_t> di(0, maximum-1);
81  return chrono::milliseconds(di(g_random_engine));
82 }
static std::mutex mutex
Definition: Logger.cc:43
STL namespace.
static uint32_t number32(uint32_t maximum=0)
Returns a random 32-bit unsigned integer.
Definition: Random.cc:55
#define THREAD_LOCAL
Definition: Random.cc:38
static int64_t number64(int64_t maximum=0)
Returns a random 64-bit unsigned integer.
Definition: Random.cc:63
Compatibility Macros for C/C++.
#define LOCK_GLOBAL_MUTEX(m)
Definition: Random.cc:39
Hypertable definitions
static double uniform01()
Returns a random double.
Definition: Random.cc:72
Random number generator for int32, int64, double and ascii arrays.
static std::chrono::milliseconds duration_millis(uint32_t maximum)
Returns a random millisecond duration.
Definition: Random.cc:77
static void seed(unsigned int s)
Sets the seed of the random number generator.
Definition: Random.cc:50