MostlyHarmless 0.0.1
 
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
43 template <NumericType T>
44 [[nodiscard]] T generate(Range<T> range) noexcept {
45 Distribution<T> distribution{ range.min, range.max };
46 const auto res = distribution(m_rng);
47 return res;
48 }
49
50
51 private:
52 template <class T>
53 using Distribution = std::conditional_t<std::is_floating_point_v<T>,
54 std::uniform_real_distribution<T>,
55 std::uniform_int_distribution<T>>;
56 Engine m_rng;
57 };
58
59 // Default, to not break existing codebases..
61
62
63} // namespace marvin::utils
64#endif
A class for (pseudo) random number generation.
Definition marvin_Random.h:30
T generate(Range< T > range) noexcept
Definition marvin_Random.h:44
RandomGenerator(std::random_device &rd)
Utility helper functions and classes.
Definition marvin_Utils.h:21
RandomGenerator< std::mt19937 > Random
Definition marvin_Random.h:60
POD type that represents a range of values, for classes requiring a min and a max.
Definition marvin_Range.h:21