-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.cpp
71 lines (64 loc) · 2.74 KB
/
random.cpp
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
#include "random.h"
using namespace std;
static boost::mt19937 generator(static_cast<unsigned> (std::time(0)));
//static boost::mt19937 generator(static_cast<unsigned> (10));
/*! \fn double random_uni01()
* \brief A function to generate a uniform random number between 0 and 1.
* \return a double
*/
double random_uni01() {
boost::uniform_real<> uni_dist(0,1);
boost::variate_generator<boost::mt19937&, boost::uniform_real<> > uni(generator, uni_dist);
return uni();
}
// return a random number between lowest(including) and highest(excluding)
/*! \fn unsigned int get_a_random_number(int lowest, int highest)
* \brief A function to return a random number between lowest(including) and highest(excluding).
* \param lowest an integer
* \param highest an integer
* \return an unsigned integer
*/
unsigned int get_a_random_number(int lowest, int highest) {
if (highest < lowest) {
cout << "ERROR In random_number_generator: Higher value is smaller than lower" << endl;
exit(1);
}
unsigned int random_integer;
int range=(highest-lowest);
random_integer = lowest + rand()%range;
return random_integer;
}
// return a random number between lowest(including) and highest(excluding) using boost
/*! \fn unsigned int boost_get_a_random_number(int lowest, int highest)
* \brief A function to return a random number between lowest(including) and highest(excluding) using boost.
* \param lowest an integer
* \param highest an integer
* \return an unsigned integer
*/
unsigned int boost_get_a_random_number(int lowest, int highest) {
if (highest < lowest) {
cout << "ERROR In random_number_generator: Higher value is smaller than lower" << endl;
exit(1);
}
//boost::mt19937 rng;
boost::uniform_int<> range_dist(lowest,highest-1);
boost::variate_generator<boost::mt19937&, boost::uniform_int<> >
int_ran_gen(generator, range_dist); // glues randomness with mapping
return int_ran_gen();
}
/*! \fn unsigned int randomWithDiscreteProbability(const vector<double>& accum_prob_vec)
* \brief A function to return a random number with discrete probability; pass the cum. distribution vector.
* \param accum_prob_vec a constant reference of double vector.
* \return an unsigned integer
*/
unsigned int randomWithDiscreteProbability(const vector<double>& accum_prob_vec) {
double x = random_uni01();
return lower_bound(accum_prob_vec.begin(), accum_prob_vec.end(), x) -
accum_prob_vec.begin();
}
unsigned int randomWithDiscreteProbability(const vector<int>& accum_prob_vec) {
int highest = accum_prob_vec.back()+1;
int x = boost_get_a_random_number(0, highest);
return lower_bound(accum_prob_vec.begin(), accum_prob_vec.end(), x) -
accum_prob_vec.begin();
}