-
Notifications
You must be signed in to change notification settings - Fork 0
/
Misc_Random.hpp
108 lines (95 loc) · 2.69 KB
/
Misc_Random.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Misc_Random.hpp
*
* Revision: October 2018
* Author: Thassyo Pinto - [email protected]
*
* A collection of functions for creating random doubles.
*/
#ifndef MISC_RANDOM_HPP_
#define MISC_RANDOM_HPP_
// Standard libraries
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iostream>
// Seeds the random number generator with the indicated seed.
inline void seed(unsigned int seed){
#ifdef DEBUG
std::cout << "Seed set: " << seed << std::endl;
#endif
srand(seed);
}
// Seeds the random number generator based on the current time.
inline void seed(){
time_t current_time = time(NULL);
#ifdef DEBUG
std::cout << "Seeded by time " << current_time << std::endl;
#endif
srand(current_time);
}
// Returns a random double in the interval [0, 1].
inline double randDouble(){
double random = (double)rand()/(double)RAND_MAX;
#ifdef DEBUG
std::cout << "randDouble: " << random << std::endl;
#endif
return random;
}
// Returns a random double in the interval [0, max].
inline double randDouble(double max){
double random = randDouble() * max;
#ifdef DEBUG
std::cout << "randDouble(max): " << random << std::endl;
#endif
return random;
}
// Returns a random double in the interval [min, max].
inline double randDouble(double min, double max){
double random = randDouble() * (max - min) + min;
#ifdef DEBUG
std::cout << "randDouble(min,max): " << random << std::endl;
#endif
return random;
}
// Returns a random integer in the interval [min, max].
inline int randInt(int min, int max){
int randomIndex = int(randDouble(min, max));
while(randomIndex >= max){
randomIndex = int(randDouble(min, max));
}
#ifdef DEBUG
std::cout << "randInt: " << randomIndex << std::endl;
#endif
return randomIndex;
}
// Returns a random integer in the interval [0, max].
inline int randInt(int max){
return randInt(0, max);
}
// Gaussian random function to produce a real number according to a Gaussian distribution.
inline double randGaussian(double m=0.0,double v=1.0){
double factor = sqrt(-2.0f * log(randDouble()));
float angle = 2.0f * M_PI * randDouble();
double random = double(m + v * factor * cos(angle));
#ifdef DEBUG
std::cout << "randGaussian: " << random << std::endl;
#endif
return random;
}
// Generates a random index in the range [min, max].
inline size_t randIndex(size_t min, size_t max){
size_t randomIndex = size_t(randDouble(min, max));
while(randomIndex >= max){
randomIndex = size_t(randDouble(min, max));
}
#ifdef DEBUG
std::cout << "randIndex: " << randomIndex << std::endl;
#endif
return randomIndex;
}
// Generates a random index in the range [0, max].
inline size_t randIndex(size_t max){
return randIndex(0, max);
}
#endif /* MISC_RANDOM_HPP_ */