summaryrefslogtreecommitdiffhomepage
path: root/src/random.hpp
blob: e8e462e81b9c1df4d0a6354e6685d7c7a8f0adb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#pragma once

#include <cstddef>
#include <cstdint>
#include <concepts>
#include <type_traits>

namespace floormat {

struct random_engine
{
    virtual ~random_engine();
    virtual std::common_type_t<std::size_t, std::uintptr_t, std::ptrdiff_t> operator()() = 0;

    template<std::integral T>
    requires (sizeof(T) <= sizeof(std::size_t))
    T operator()(T max) {
        return static_cast<T>(operator()() % static_cast<std::size_t>(max));
    }

    template<std::integral T>
    requires (sizeof(T) <= sizeof(std::size_t))
    T operator()(T min, T max) {
        return min + operator()(max-min);
    }

    virtual float operator()(float min, float max) = 0;
};

[[maybe_unused]] extern random_engine& random; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)

} // namespace floormat