-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRNG.cpp
72 lines (56 loc) · 1.45 KB
/
RNG.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
72
#include <cmath>
#include <ctime>
#include <boost/random.hpp>
#include <boost/math/constants/constants.hpp>
#include "float3.h"
#include "RNG.h"
using namespace std;
static boost::mt19937 rng;
static bool is_initialized = false;
void init_rng()
{
if (is_initialized)
return;
is_initialized = true;
rng.seed(time(NULL));
}
void init_rng(boost::uint32_t seed)
{
is_initialized = true;
rng.seed(seed);
}
// thanks http://www.bnikolic.co.uk/blog/cpp-boost-uniform01.html for pitfall warning
float randfloat()
{
static boost::uniform_01<boost::mt19937> zeroone(rng);
return zeroone();
}
float randfloat(float start, float end)
{
boost::uniform_real<float> rnd(start, end);
boost::variate_generator<boost::mt19937&, boost::uniform_real<float> >
gimme_random(rng, rnd);
return gimme_random();
}
int randint(int start, int end)
{
boost::uniform_int<> rnd(start, end);
boost::variate_generator<boost::mt19937&, boost::uniform_int<> >
gimme_random(rng, rnd);
return gimme_random();
}
float3 random_direction()
{
float x = randfloat(0, 2*boost::math::constants::pi<float>());
return float3(sin(x), 0, cos(x));
}
float3 random_offset_pos(const float3& basePos, float minoffset, float maxoffset)
{
float3 dest;
do {
float r = randfloat(minoffset, maxoffset);
float3 modDir = random_direction();
dest = basePos + modDir * r;
} while (!dest.IsInBounds());
return dest;
}