VIPRA Documentation
Loading...
Searching...
No Matches
random.hpp
1#pragma once
2
3#include <algorithm>
4#include <cassert>
5#include <cstdint>
6#include <cstdlib>
7#include <limits>
8#include <random>
9#include <vector>
10
11#include "vipra/concepts/numeric.hpp"
12#include "vipra/types/float.hpp"
13#include "vipra/types/size.hpp"
14
15namespace VIPRA::Random {
16
21// NOLINTNEXTLINE (rolland) Default Naming Convention looks ugly
22class Engine {
23 using seed = __uint128_t;
24
25 public:
26 using result_type = uint64_t;
27
28 Engine() : _currVal(DEFAULT_SEED) {}
29 explicit Engine(uint64_t seedVal) : _currVal(seedVal)
30 {
31 assert(seedVal != 0);
32
33 _currVal <<= SHIFT_NUM;
34 }
35
36 static constexpr uint64_t DEFAULT_SEED = 12345;
37
43 auto operator()() noexcept -> uint64_t
44 {
45 assert(_currVal != 0);
46
47 _currVal *= MULT_NUM;
48 return _currVal >> SHIFT_NUM;
49 }
50
56 void reseed(uint64_t seedNum) noexcept
57 {
58 _currVal = seedNum;
59 _currVal <<= SHIFT_NUM;
60
61 assert(_currVal != 0);
62 }
63
69 static inline constexpr auto min() noexcept -> uint64_t { return 0; }
70
76 static inline constexpr auto max() noexcept -> uint64_t
77 {
78 return std::numeric_limits<uint64_t>::max();
79 }
80
81 private:
82 seed _currVal;
83 static constexpr uint64_t MULT_NUM = 0xda942042e4dd58b5;
84 static constexpr uint64_t SHIFT_NUM = 64;
85
86 public:
87 ~Engine() = default;
88 Engine(const Engine&) = default;
89 Engine(Engine&&) noexcept = default;
90 auto operator=(const Engine&) -> Engine& = default;
91 auto operator=(Engine&&) noexcept -> Engine& = default;
92};
93
104template <typename dist_t, Concepts::Numeric data_t = VIPRA::f_pnt>
105inline auto make_distribution(dist_t&& distr, VIPRA::size count,
106 VIPRA::Random::Engine& engine) -> std::vector<data_t>
107{
108 std::vector<data_t> ret(count);
109
110 std::transform(ret.cbegin(), ret.cend(), ret.begin(), [&](data_t) {
111 data_t val = distr(engine);
112 return val;
113 });
114
115 return ret;
116}
117
118} // namespace VIPRA::Random
Psuedo Random number engine.
Definition random.hpp:22
static constexpr auto min() noexcept -> uint64_t
Returns the minimum number possible.
Definition random.hpp:69
void reseed(uint64_t seedNum) noexcept
Restarts the number generator with a new seed.
Definition random.hpp:56
auto operator()() noexcept -> uint64_t
Produces the next pseudo random number in the sequence, uses Lehmer's generator.
Definition random.hpp:43
static constexpr auto max() noexcept -> uint64_t
Returns the maximum number possible.
Definition random.hpp:76