-
Notifications
You must be signed in to change notification settings - Fork 0
/
libEDM_random.h
82 lines (58 loc) · 2.53 KB
/
libEDM_random.h
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#pragma once
#include <complex>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <boost/cstdint.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_01.hpp>
#include <boost/random/uniform_int.hpp>
#include <libEDM_types.h>
#include <libEDM_matrix.h>
using std::complex;
using std::time;
using boost::uint32_t;
using boost::mt19937;
using boost::uniform_01;
using boost::uniform_int;
class Random {
public:
// constructor
Random (uint32_t seed = time(0)) : generator(seed), iset(false), uniform01rng(generator) {}
// generates random number between (0, 1]
double sample () { return uniform01rng(); }
// generates a random angle between (-PI, PI]
radians angle ();
// generates uniform random number between (low, high)
int uniform (int low, int high);
// generates a Gaussian random number with specified mean and variance
double gaussian (double mean, double stddev) { return mean + (stddev == 0.0 ? 0.0 : stddev * gaussian()); }
double gaussian ();
// generates a complex Gaussian random number with specified mean and variance
complex<double> complex_gaussian (const complex<double> &mean, double stddev) { return mean + (stddev == 0.0 ? 0.0 : stddev * complex_gaussian()); }
complex<double> complex_gaussian ();
// lognormal distribution
double lognormal( const double mu, const double sigma ) {return std::exp( gaussian(mu, sigma) );}
void lognormal_params( const double mean, const double variance, double &mu, double &sigma );
// generates a Poisson random number
size_t poisson (double mean);
// generates a Rayleigh random number
double rayleigh (double stddev = 1.0) {return norm(complex_gaussian(0.0, stddev));}
// generates an Exponential random number
double exponential (const double mu) {return -mu * std::log( 1.0 - sample() );}
// Pareto distribution
double pareto ( const double alpha, const double min ) {return min * std::pow( sample(), -1.0 / alpha );}
double pareto_min ( const double k, const double mean ) {return ( k - 1.0 ) * mean / k;}
// generates a random bit
bool bit () { return sample() < 0.5; }
// generates a vector of random bits
bVector bit_vector (const size_t length);
// set seed of random generator
void set_seed (const uint32_t seed);
private:
mt19937 generator;
uniform_01<mt19937> uniform01rng;
bool iset;
double gset;
};
extern Random globalRandom;