-
Notifications
You must be signed in to change notification settings - Fork 0
/
rand.hpp
96 lines (73 loc) · 2.25 KB
/
rand.hpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#pragma once
//Libraries
#ifdef TEENSYDUINO
#include<Entropy.h>
#endif
#ifdef NOISE_PC_SYSTEM
#include <random>
#include <cmath>
#endif
/** @brief Provides a common interface for random number generation for Embedded system implementation and PC runs.
* Imulates integer random() function for PC Systems to maintain constant behaviour across */
namespace Rand
{
unsigned int Walker_Size = 0;
unsigned int Edge = 0;
unsigned int StepSize = 0;
#ifdef NOISE_PC_SYSTEM
std::mt19937 Engine; //MT PRNG
std::uniform_real_distribution<double> Uni_dist(0.0, 1.0);
/** @brief Integer random function emulator - [lower, upper).
* \attention `upper` is an open limit. */
int random(lower, upper) //overloads for emulating an int based distributions on PC, as seen on Arduino.
{
double result = lower + (Rand::Uni_dist(Rand::Engine()) * (upper-lower));
result = std::floor(result);
return int(result);
}
#endif
void init(unsigned int walker_size, unsigned int edge, unsigned int max_step_size)
{
//Attention - No input validation
Rand::Walker_Size = walker_size;
Rand::Edge = edge;
Rand::step_size = max_step_size + 1; //To compensate for the open limit
#ifdef NOISE_EMBEDDEDSYSTEMS //EMBEDDED Specific Implementation
#ifdef TEENSYDUINO
Entropy.Initialize();
uint32_t seed_value = Entropy.random();
randomSeed(seed_value);
#else
uint32_t seed_value = analogRead(NOISE_EMPTY_ENTROPY_PIN);
randomSeed(seed_value);
#endif //TEENSYDUINO
#elif defined(NOISE_PC_SYSTEM)
std::random_device r;
Rand::Engine.seed(r());
std::cerr << "Seed used: \n";
#endif
} //End of Rand::init()
/** @brief Returns a random sign with 0.5 probability each. */
int sign()
{
unsigned int temp = random(0,2);
temp += !(temp)*-1;
//if temp is 0 : 0 → -1, 1 → 1.
return temp;
}
/** @brief Returns a dynamically generated step size. */
int step()
{
return random(0, Rand::StepSize);
}
/** @brief Return a random component between the edges. */
int in_edge()
{
return random(0, Rand::Edge);
}
/** @brief Return a Random PartID/WalkerID*/
unsigned int partid()
{
return random(0, Rand::Walker_Size);
}
}; //End of namespace Rand