marvin 0.0.1
Pure C++ audio helper library
Loading...
Searching...
No Matches
marvin_Random.h
Go to the documentation of this file.
1// ========================================================================================================
2// _______ _______ ______ ___ ___ _______ _______
3// | | | _ | __ \ | |_ _| | |
4// | | | < | |_| |_| |
5// |__|_|__|___|___|___|__|\_____/|_______|__|____|
6//
7// This file is part of the Marvin open source library and is licensed under the terms of the MIT License.
8//
9// ========================================================================================================
10
11#ifndef SLMADSP_RANDOM_H
12#define SLMADSP_RANDOM_H
15#include <random>
16namespace marvin::utils {
29 template <RandomEngineType Engine>
30 class RandomGenerator final {
31 public:
36 explicit RandomGenerator(std::random_device& rd);
37
42 auto setSeed(std::seed_seq seedSequence) -> void;
43
48 auto setSeed(int seed) -> void;
49
55 template <NumericType T>
56 [[nodiscard]] T generate(Range<T> range) noexcept {
57 Distribution<T> distribution{ range.min, range.max };
58 const auto res = distribution(m_rng);
59 return res;
60 }
61
62
63 private:
64 template <class T>
65 using Distribution = std::conditional_t<std::is_floating_point_v<T>,
66 std::uniform_real_distribution<T>,
67 std::uniform_int_distribution<T>>;
68 Engine m_rng;
69 };
70
71 // Default, to not break existing codebases..
73
74
75} // namespace marvin::utils
76#endif
A class for (pseudo) random number generation.
Definition marvin_Random.h:30
auto setSeed(int seed) -> void
T generate(Range< T > range) noexcept
Definition marvin_Random.h:56
auto setSeed(std::seed_seq seedSequence) -> void
RandomGenerator(std::random_device &rd)
Utility helper functions and classes.
Definition marvin_Utils.h:21
RandomGenerator< std::mt19937 > Random
Definition marvin_Random.h:72
POD type that represents a range of values, for classes requiring a min and a max.
Definition marvin_Range.h:21