LIPH's C++ Codes
random.h
Go to the documentation of this file.
1#ifndef LIPH_NUMERIC_RANDOM_H_
2#define LIPH_NUMERIC_RANDOM_H_
3
4#include <cstdint>
5#include <random>
6#include <stdexcept>
7#include <string>
8#include <string_view>
9
10namespace liph {
11
12struct random {
14 static bool next_bytes(void *ptr, size_t n);
15
17 static int32_t next_int32() { return next<int32_t>(); }
18
20 static uint32_t next_uint32() { return next<uint32_t>(); }
21
23 static int64_t next_int64() { return next<int64_t>(); }
24
26 static uint64_t next_uint64() { return next<uint64_t>(); }
27
29 static float next_float() { return next<float>(); }
30
32 static double next_double() { return next<double>(); }
33
36 static int32_t gen_int(int32_t n) { return n <= 0 ? 0 : (next_uint32() / 2) % n; }
37
40 static int32_t gen_int(int32_t a, int32_t b) { return a + gen_int(b - a); }
41
43 static double gen_real() { return (next_uint64() >> 11) * (1.0 / 9007199254740992.0); }
44
46 static std::string gen_str(size_t size, std::string_view charset = "0123456789");
47
48private:
49 template <class T>
50 static T next() {
51 T ret;
52 if (!next_bytes(&ret, sizeof(T))) throw std::runtime_error("fetch next random bytes fail");
53 return ret;
54 }
55};
56
57#if 0
58class randomizer {
59public:
60 explicit randomizer(uint64_t n) : n_(n), d_(0, n_) {}
61
63 uint64_t operator()() { return d_(e); }
64
65private:
66 int n_;
67 std::uniform_int_distribution<uint64_t> d_;
68
69 static std::random_device r;
70 static std::default_random_engine e;
71};
72#endif
73
74class rand {
75public:
76#if 0
77 rand(uint32_t seed = 1) { ::srand(seed); }
78 uint32_t operator()() { return ::rand(); }
79#endif
80
81 rand(uint32_t seed = 1) : seed_(seed & 0x7fffffffu) {
82 if (seed_ == 0 || seed_ == 2147483647L) {
83 seed_ = 1;
84 }
85 }
86
88 uint32_t operator()() {
89 static const uint32_t M = 2147483647L;
90 static const uint64_t A = 16807;
91 // seed_ = (seed_ * A) % M
92 uint64_t product = seed_ * A;
93 seed_ = static_cast<uint32_t>((product >> 31) + (product & M));
94 if (seed_ > M) {
95 seed_ -= M;
96 }
97 return seed_;
98 }
99
100 static uint32_t min() { return 1; } // seed: 1407677000
101 static uint32_t max() { return 2147483646L; } // seed: 739806647
102
103private:
104 uint32_t seed_;
105};
106
107} // namespace liph
108
109#endif // LIPH_NUMERIC_RANDOM_H_
Definition: random.h:74
rand(uint32_t seed=1)
Definition: random.h:81
uint32_t operator()()
generate random number range [min(), max()]
Definition: random.h:88
static uint32_t max()
Definition: random.h:101
static uint32_t min()
Definition: random.h:100
Definition: algorithm.h:10
Definition: random.h:12
static float next_float()
generate random float
Definition: random.h:29
static int32_t gen_int(int32_t a, int32_t b)
Definition: random.h:40
static std::string gen_str(size_t size, std::string_view charset="0123456789")
generate random string of length size from charset
Definition: random.cpp:34
static double next_double()
generate random double
Definition: random.h:32
static uint64_t next_uint64()
generate random uint64_t
Definition: random.h:26
static int64_t next_int64()
generate random int64_t
Definition: random.h:23
static uint32_t next_uint32()
generate random uint32_t
Definition: random.h:20
static int32_t next_int32()
generate random int32_t
Definition: random.h:17
static int32_t gen_int(int32_t n)
Definition: random.h:36
static double gen_real()
generate double range [0, 1)
Definition: random.h:43
static bool next_bytes(void *ptr, size_t n)
generate random n bytes
Definition: random.cpp:16