Skip to content

Commit ef401d5

Browse files
committed
make random::generate(x, y) be able to accept return type as first template param
1 parent e1ad7fc commit ef401d5

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

loader/include/Geode/utils/random.hpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,19 @@ namespace geode::utils::random {
5252

5353
/// Generates a random value of type T, in the range [min, max),
5454
/// meaning the max value can never be returned
55-
template <typename T, typename Y, typename Out = std::common_type_t<T, Y>>
56-
Out generate(T min, Y max) {
55+
template <typename R = void, typename T, typename Y>
56+
auto generate(T min_, Y max_) {
57+
using Out = std::conditional_t<std::is_void_v<R>, std::common_type_t<T, Y>, R>;
58+
59+
Out min = static_cast<Out>(min_);
60+
Out max = static_cast<Out>(max_);
61+
5762
if (max <= min) return min;
5863

5964
if constexpr (std::is_integral_v<Out>) {
6065
Out range = max - min;
6166
uint64_t val = nextU64();
62-
return static_cast<Out>(min) + (val % range);
67+
return static_cast<Out>(static_cast<Out>(min) + (val % range));
6368
} else if constexpr (std::is_floating_point_v<Out>) {
6469
double range = static_cast<double>(max) - static_cast<double>(min);
6570
double val = generate<double>();

0 commit comments

Comments
 (0)