30__pragma(warning(push))
31__pragma(warning(disable:4244))
56#ifndef VCTR_RANDOM_SEED
57#error "VCTR_RANDOM_SEED must be defined to a positive integer to use compile_time_random_array.h, e.g. -DVCTR_RANDOM_SEED=1234"
60namespace vctr::random::detail
67consteval uint32_t uniformDistribution (uint32_t& previous)
69 constexpr uint32_t lce_a = 48271;
70 constexpr uint32_t lce_c = 0;
71 constexpr uint32_t lce_m = std::numeric_limits<int32_t>::max();
72 previous = ((lce_a * previous + lce_c) % lce_m);
78consteval T uniformDistribution (T
min, T
max, uint32_t& seed)
80 auto span = double (
max -
min);
81 constexpr auto scale = double (std::numeric_limits<int32_t>::max());
82 return T (
double (uniformDistribution (seed)) / scale * (span)) +
min;
89template <
size_t size,
typename T>
90consteval auto makeArrayWithUniformDistribution (T
min, T
max, uint32_t seed)
92 std::array<T, size> dst {};
94 el = uniformDistribution (
min,
max, seed);
105template <
size_t size, std::
floating_po
int T>
106consteval auto makeArrayWithNormalDistribution (T
mean, T sigma, uint32_t seed)
108 std::array<T, size> dst {};
110 for (
size_t i = 0; i < size - 1; i += 2)
119 u1 = uniformDistribution (T (0), T (1), seed);
120 u2 = uniformDistribution (T (0), T (1), seed);
121 }
while (u1 <= std::numeric_limits<T>::epsilon());
130 auto mag = sigma * gcem::sqrt (T (-2) * gcem::log (u1));
131 auto twoPiU2 = std::numbers::pi_v<T> * 2 * u2;
132 dst[i] = mag * gcem::cos (twoPiU2) +
mean;
136 dst[i + 1] = mag * gcem::sin (twoPiU2) +
mean;
143namespace vctr::random
147consteval uint32_t getSeed()
149 return static_cast<uint32_t
> (VCTR_RANDOM_SEED);
157template <
size_t size,
typename T>
158consteval auto makeArrayWithUniformDistribution (T
min, T
max, uint32_t seedOffset = 0)
160 return detail::makeArrayWithUniformDistribution<size, T> (
min,
max, getSeed() + seedOffset);
166template <
size_t size, std::
floating_po
int T>
167consteval auto makeArrayWithNormalDistribution (T
mean, T sigma)
169 return detail::makeArrayWithNormalDistribution<size, T> (
mean, sigma, getSeed());
constexpr ExpressionChainBuilder< expressions::Mean > mean
Computes the mean value of the source values.
Definition: Mean.h:252
constexpr ExpressionChainBuilder< expressions::Max > max
Computes the maximum value of the source values.
Definition: Max.h:198
constexpr ExpressionChainBuilder< expressions::Min > min
Computes the minimum value of the source values.
Definition: Min.h:198