-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
61 lines (55 loc) · 1.06 KB
/
util.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
/*
* File: util.cpp
* Authors: Alexander Epp, Rain Epp
* Project: CMPUT274 Final Project
* Description: Implementation of utility functions
*/
#include "util.h"
#include "vec.h"
namespace util
{
uint32_t _lcrg_x_;
// Linear Congruential Random Generator
// https://en.wikipedia.org/wiki/Linear_congruential_generator
static uint32_t lcrg()
{
static const uint32_t a = 1103515245;
static const uint32_t c = 12345;
return _lcrg_x_ = a*_lcrg_x_ + c; // Automatic modulus
}
// Seed lcrg
static void slcrg(uint32_t s)
{
debugPrint("Seeding lcrg: ", s);
_lcrg_x_ = s;
}
int32_t random(int32_t maxval)
{
return lcrg() % maxval;
}
void srand(uint32_t seed)
{
slcrg(seed);
}
#ifdef ARDUINO_BUILD
/*int32_t random(int32_t max)
{
return ::random(max);
}*/
void srand()
{
slcrg(getSeed<int32_t, sizeof(int32_t)>(0, 50));
//::randomSeed(getSeed<int32_t, sizeof(int32_t)>(0, 50));
}
#else
/*int32_t random(int32_t maxval)
{
return std::rand()%maxval;
}*/
void srand()
{
slcrg(std::time(0));
//std::srand(std::time(0));
}
#endif
}