-
Notifications
You must be signed in to change notification settings - Fork 2
/
random.hpp
52 lines (39 loc) · 1.4 KB
/
random.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//This file contains a number of functions for getting seeding random number
//generators and pulling numbers from them in a thread-safe manner.
#ifndef _richdem_random_hpp_
#define _richdem_random_hpp_
#include <random>
#include <string>
///Maximum number of threads this class should deal with
const int PRNG_THREAD_MAX = 200;
#ifdef _OPENMP
#include <omp.h>
#else
#define omp_get_thread_num() 0
#define omp_get_num_threads() 1
#define omp_get_max_threads() 1
#endif
typedef std::string RandomEngineState;
typedef std::mt19937 our_random_engine;
//Returns a PRNG engine specific to the calling thread
our_random_engine& rand_engine();
//Seeds the PRNG engines using entropy from the computer's random device
void seed_rand(unsigned long seed);
//Returns an integer value on the closed interval [from,thru]
//Thread-safe
int uniform_rand_int(int from, int thru);
//Returns an floating-point value on the interval [from,thru)
//Thread-safe
double uniform_rand_real(double from, double thru);
//Returns a Gaussian-distributed value with specified mean and standard
//deviation. Thread-safe
double normal_rand(double mean, double stddev);
template<class T>
T uniform_bits(){
std::uniform_int_distribution<T>
dist(std::numeric_limits<T>::lowest(),std::numeric_limits<T>::max());
return dist( rand_engine() );
}
RandomEngineState SaveRandomState();
void SetRandomState(const RandomEngineState &res);
#endif