-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomNum.cpp
48 lines (34 loc) · 1.48 KB
/
randomNum.cpp
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
//
// Created by Vladas on 2019-05-21.
//
#include "randomNum.h"
#include <random>
#include <chrono>
#include <iomanip>
std::mt19937 randomly_seeded_engine() {
// create a seed sequence of several reasonably random values
std::seed_seq seed_seq{(unsigned int) std::random_device{}(),
(unsigned int) std::chrono::system_clock::now().time_since_epoch().count(),
(unsigned int) std::random_device{}(),
(unsigned int) std::chrono::steady_clock::now().time_since_epoch().count(),
(unsigned int) std::random_device{}()};
return std::mt19937(seed_seq); // note: the seed sequence provides a warm up sequence for the rng
}
double randomNum(double &a, double &b) {
static auto rng = randomly_seeded_engine();
static std::uniform_real_distribution<double> distribution;
distribution.param(std::uniform_real_distribution<double>::param_type{a, b});
return distribution(rng);
}
int randomNum(int a, int b) {
static auto rng = randomly_seeded_engine();
static std::uniform_int_distribution<int> distribution;
distribution.param(std::uniform_int_distribution<int>::param_type{a, b});
return distribution(rng);
}
double randomNum() {
static auto rng = randomly_seeded_engine();
static std::uniform_real_distribution<double> distribution;
distribution.param(std::uniform_real_distribution<double>::param_type{0, 1});
return distribution(rng);
}