-
Notifications
You must be signed in to change notification settings - Fork 3
/
random.cpp
52 lines (43 loc) · 1017 Bytes
/
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
/*! \file
\brief Definitions of functions declared in random.hpp
*/
#include "random.hpp"
#include <boost/random.hpp>
#include <algorithm>
#include <assert.h>
using std::lower_bound;
typedef boost::uniform_01<OurGenerator, double> Our01;
static Our01 * r = 0;
int RandomInt(int limit)
{
return (int)((*r)() * limit);
}
int RandomInt8020(int limit)
{
int cut = limit / 5;
if (RandomInt(5) == 0)
return RandomInt(limit - cut) + cut;
else
return RandomInt(cut);
}
/*! If floor(avg) == ceil(avg) then that number will be returned 100% of the time. */
int RandomConsecutive(double avg)
{
double fl = floor(avg);
if ((*r)() < avg - fl)
return (int)ceil(avg);
else
return (int)fl;
}
int RandomDiscrete(vector<double> const & cf)
{
double d = (*r)();
assert(d <= cf[cf.size() - 1]);
return lower_bound(cf.begin(), cf.end(), d) - cf.begin();
}
void RandomSeed(OurGenerator::result_type seed)
{
static OurGenerator generator(seed);
static Our01 rnd(generator);
r = &rnd;
}