-
Notifications
You must be signed in to change notification settings - Fork 0
/
dkstd_random.hpp
101 lines (84 loc) · 2.27 KB
/
dkstd_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
// author: Khiêm Đoàn Hoà (KhiemDH)
// github: https://github.com/khiemdoan/dkstd
// created: 2018-12-18
// modified: 2019-08-01
#pragma once
#include <climits>
#include <random>
#include <vector>
#include <list>
#include <algorithm>
namespace dkstd {
namespace random {
double rand();
double rand(double min, double max);
int randint();
int randint(int min, int max);
template<typename T>
T choice(const std::vector<T>& seq);
template<typename T>
T choice(const std::list<T>& seq);
template<typename T>
void shuffle(std::vector<T>& seq);
template<typename T>
void shuffle(std::list<T>& seq);
}
}
// KhiemDH - 2019-07-31
double dkstd::random::rand()
{
return dkstd::random::rand(0.0, 1.0);
}
// KhiemDH - 2019-08-01
double dkstd::random::rand(double min, double max)
{
std::random_device device;
std::default_random_engine engine{ device() };
std::uniform_real_distribution<> dist(min, max);
return dist(engine);
}
// KhiemDH - 2019-07-31
int dkstd::random::randint() {
return dkstd::random::randint(INT_MIN, INT_MAX);
}
// KhiemDH - 2019-08-01
int dkstd::random::randint(int min, int max) {
std::random_device device;
std::default_random_engine engine{device()};
std::uniform_int_distribution<> dist(min, max);
return dist(engine);
}
// KhiemDH - 2019-07-31
template<typename T>
T dkstd::random::choice(const std::vector<T>& seq)
{
auto index = randint(0, seq.size() - 1);
auto li = seq.begin();
std::advance(li, index);
return *li;
}
// KhiemDH - 2019-07-31
template<typename T>
T dkstd::random::choice(const std::list<T>& seq)
{
auto index = randint(0, seq.size() - 1);
auto vi = seq.begin();
std::advance(vi, index);
return *vi;
}
// KhiemDH - 2019-07-31
template<typename T>
void dkstd::random::shuffle(std::vector<T>& seq)
{
std::random_device device;
std::default_random_engine engine{ device() };
std::shuffle(seq.begin(), seq.end(), engine);
}
// KhiemDH - 2019-07-31
template<typename T>
void dkstd::random::shuffle(std::list<T>& seq)
{
std::random_device device;
std::default_random_engine engine{ device() };
std::shuffle(seq.begin(), seq.end(), engine);
}