Brush C++ API
A flexible interpretable machine learning framework
Loading...
Searching...
No Matches
rnd.cpp
Go to the documentation of this file.
1/* FEAT
2copyright 2017 William La Cava
3license: GNU/GPL v3
4*/
5
6#include "rnd.h"
7
8namespace Brush { namespace Util{
9
10 Rnd* Rnd::instance = NULL;
11
13 {
21 // TODO: stop using omp. this should be based on number of islands. make each island to use their respective
22 // when we resize, the constructor of new elements are invoked.
23 rg.resize(omp_get_max_threads());
24 }
25
27 {
28 // creates the static random generator by calling the constructor
29 if (!instance)
30 {
31 instance = new Rnd();
32 instance->set_seed(0); // setting a random initial state
33 }
34
35 return instance;
36 }
37
39 {
40 if (instance)
41 delete instance;
42
43 instance = NULL;
44 }
45
46 void Rnd::set_seed(unsigned int seed)
47 {
51 if (seed == 0) {
52 // use a non-deterministic random generator to seed the deterministics
53 std::random_device rd;
54 seed = rd();
55 }
56
57 // generating a seed sequence
58 std::seed_seq seq{seed};
59
60 std::vector<std::uint32_t> seeds(rg.size());
61 seq.generate(seeds.begin(), seeds.end());
62
63 for (size_t i = 0; i < rg.size(); ++i) {
64 rg[i].seed(seeds[i]);
65 }
66 }
67
69 {
70 std::uniform_int_distribution<> dist( lowerLimit, upperLimit );
71 return dist(rg[omp_get_thread_num()]);
72 }
73
74 float Rnd::rnd_flt(float min, float max)
75 {
76 std::uniform_real_distribution<float> dist(min, max);
77 return dist(rg[omp_get_thread_num()]);
78 }
79
80 float Rnd::rnd_dbl(float min, float max)
81 {
82 std::uniform_real_distribution<float> dist(min, max);
83 return dist(rg[omp_get_thread_num()]);
84 }
85
86 float Rnd::operator()(unsigned i)
87 {
88 return rnd_dbl(0.0,i);
89 }
90
91 float Rnd::operator()() { return rnd_flt(0.0,1.0); }
92
94 //Returns a normally distributed deviate with zero mean and unit variance
95 {
96 float ran = rnd_flt(-1,1);
97 static int iset=0;
98 static float gset;
99 float fac,rsq,v1,v2;
100 if (iset == 0) {// We don't have an extra deviate handy, so
101 do{
102 v1=float(2.0*rnd_flt(-1,1)-1.0); //pick two uniform numbers in the square ex
103 v2=float(2.0*rnd_flt(-1,1)-1.0); //tending from -1 to +1 in each direction,
104 rsq=v1*v1+v2*v2; //see if they are in the unit circle,
105 } while (rsq >= 1.0 || rsq == 0.0); //and if they are not, try again.
106 fac=float(sqrt(-2.0*log(rsq)/rsq));
107 //Now make the Box-Muller transformation to get two normal deviates. Return one and
108 //save the other for next time.
109 gset=v1*fac;
110 iset=1; //Set flag.
111 return v2*fac;
112 }
113 else
114 { //We have an extra deviate handy,
115 iset=0; //so unset the flag,
116 return gset; //and return it.
117 }
118 }
119
121 vector<size_t> Rnd::shuffled_index(size_t n)
122 {
123 vector<size_t> idx(n);
124 std::iota(idx.begin(), idx.end(), 0);
125 this->shuffle(idx.begin(), idx.end());
126 return idx;
127 }
129} }
void bind_engine(py::module &m, string name)
Defines a multi-core random number generator and its operators.
Definition rnd.h:29
void shuffle(RandomAccessIterator first, RandomAccessIterator last)
Definition rnd.h:49
float rnd_flt(float min=0.0, float max=1.0)
Definition rnd.cpp:74
static Rnd * initRand()
Definition rnd.cpp:26
vector< size_t > shuffled_index(size_t n)
returns a shuffled index vector of length n
Definition rnd.cpp:121
void set_seed(unsigned int seed)
Definition rnd.cpp:46
static void destroy()
Definition rnd.cpp:38
vector< std::mt19937 > rg
Definition rnd.h:163
static Rnd * instance
Definition rnd.h:167
float rnd_dbl(float min=0.0, float max=1.0)
Definition rnd.cpp:80
float operator()()
Definition rnd.cpp:91
float gasdev()
Definition rnd.cpp:93
int rnd_int(int lowerLimit, int upperLimit)
Definition rnd.cpp:68
#define omp_get_max_threads()
Definition init.h:14
#define omp_get_thread_num()
Definition init.h:12
< nsga2 selection operator for getting the front
Definition data.cpp:12