-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomNumbers.h
More file actions
48 lines (38 loc) · 1.05 KB
/
RandomNumbers.h
File metadata and controls
48 lines (38 loc) · 1.05 KB
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
#ifndef RANDOMNUMBERS_H
#define RANDOMNUMBERS_H
#include <random>
template<typename T>
class RandomGenerator;
template<>
class RandomGenerator<int> {
private:
std::mt19937 gen;
std::uniform_int_distribution<> dis;
public:
// Default constructor
RandomGenerator() : gen(std::random_device()()), dis(0, 100) {}
// Constructor with parameters
RandomGenerator(int seed, int lower_bound, int upper_bound)
: gen(seed), dis(lower_bound, upper_bound) {}
// Generate a random number
int getRandomNumber() {
return dis(gen);
}
};
template<>
class RandomGenerator<float> {
private:
std::mt19937 gen;
std::uniform_real_distribution<float> dis;
public:
// Default constructor
RandomGenerator() : gen(std::random_device()()), dis(0.0f, 1.0f) {}
// Constructor with parameters
RandomGenerator(int seed, float lower_bound, float upper_bound)
: gen(seed), dis(lower_bound, upper_bound) {}
// Generate a random number
float getRandomNumber() {
return dis(gen);
}
};
#endif