-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathRandom.h
80 lines (67 loc) · 1.57 KB
/
Random.h
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
#pragma once
#include <random>
enum RandomType
{
RANDOM_UNIFORM = 0,
RANDOM_NORMAL = 1,
};
template <typename T = float>
class Random
{
protected:
RandomType type_ = RANDOM_UNIFORM;
std::random_device device_;
std::mt19937 generator_;
std::uniform_real_distribution<T> uniform_dist_{ 0, 1 };
std::normal_distribution<T> normal_dist_{ 0, 1 };
std::minstd_rand0 generator_fast_;
public:
Random() { set_seed(); }
void set_random_type(RandomType t) { type_ = t; }
void set_parameter(T a, T b)
{
uniform_dist_.param(decltype(uniform_dist_.param())(a, b));
normal_dist_.param(decltype(normal_dist_.param())(a, b));
}
T rand()
{
if (type_ == RANDOM_UNIFORM)
{
return uniform_dist_(generator_);
}
else if (type_ == RANDOM_NORMAL)
{
return normal_dist_(generator_);
}
return 0;
}
void set_seed()
{
generator_ = std::mt19937(device_());
}
void set_seed(unsigned int seed)
{
generator_ = std::mt19937(seed);
}
int rand_int(int n)
{
return int(rand() * n);
}
int rand_int(int n1, int n2)
{
return n1 + int(rand() * (n2 - n1));
}
std::mt19937& get_generator()
{
return generator_;
}
void rand_data(T* data, size_t size)
{
for (int i = 0; i < size; i++)
{
data[i] = rand();
}
}
};
using RandomDouble = Random<double>; //use this in usual
using RandomFloat = Random<float>; //use this in usual