Skip to the content.

:heavy_check_mark: random/adaptor.hpp

Depends on

Required by

Verified with

Code

#pragma once


#include <cassert>
#include <type_traits>

#include "internal/dev_env.hpp"


namespace uni {


template<class Engine>
struct random_adaptor {
    using result_type = typename Engine::result_type;
    using signed_result_type = typename std::make_signed_t<result_type>;

  private:
    Engine engine;

  public:
    static constexpr result_type MIN = Engine::min();
    static constexpr result_type MAX = Engine::max();

    static constexpr result_type min() noexcept(NO_EXCEPT) { return MIN; }
    static constexpr result_type max() noexcept(NO_EXCEPT) { return MAX; }

    constexpr random_adaptor(unsigned long seed = 3141592653UL) noexcept(NO_EXCEPT) { this->engine.seed(seed); };

    inline constexpr result_type operator()() noexcept(NO_EXCEPT) {
        return this->engine();
    }

    inline constexpr result_type operator()(const result_type sup) noexcept(NO_EXCEPT) {
        assert(0 < sup);
        return this->engine() % sup;
    }
    inline constexpr signed_result_type operator()(const signed_result_type min, const signed_result_type sup) noexcept(NO_EXCEPT) {
        assert(min < sup);
        return min + (*this)(sup - min);
    };

    template<class T = double>
    inline constexpr auto real() noexcept(NO_EXCEPT) {
        const auto v = static_cast<T>((this->engine() + 0.5) / (1.0 + this->max()));
        return static_cast<T>((this->operator()() + v) / (1.0 + this->max()));
    }
};


} // namespace uni
#line 2 "random/adaptor.hpp"


#include <cassert>
#include <type_traits>

#line 2 "internal/dev_env.hpp"


#ifdef LOCAL_JUDGE
    inline constexpr bool DEV_ENV = true;
    inline constexpr bool NO_EXCEPT = false;
#else
    inline constexpr bool DEV_ENV = false;
    inline constexpr bool NO_EXCEPT = true;
#endif // LOCAL_JUDGE


#if __cplusplus >= 202100L
    #define CPP20 true
    #define CPP23 true
#elif __cplusplus >= 202002L
    #define CPP20 true
    #define CPP23 false
#else
    #define CPP20 false
    #define CPP23 false
#endif
#line 8 "random/adaptor.hpp"


namespace uni {


template<class Engine>
struct random_adaptor {
    using result_type = typename Engine::result_type;
    using signed_result_type = typename std::make_signed_t<result_type>;

  private:
    Engine engine;

  public:
    static constexpr result_type MIN = Engine::min();
    static constexpr result_type MAX = Engine::max();

    static constexpr result_type min() noexcept(NO_EXCEPT) { return MIN; }
    static constexpr result_type max() noexcept(NO_EXCEPT) { return MAX; }

    constexpr random_adaptor(unsigned long seed = 3141592653UL) noexcept(NO_EXCEPT) { this->engine.seed(seed); };

    inline constexpr result_type operator()() noexcept(NO_EXCEPT) {
        return this->engine();
    }

    inline constexpr result_type operator()(const result_type sup) noexcept(NO_EXCEPT) {
        assert(0 < sup);
        return this->engine() % sup;
    }
    inline constexpr signed_result_type operator()(const signed_result_type min, const signed_result_type sup) noexcept(NO_EXCEPT) {
        assert(min < sup);
        return min + (*this)(sup - min);
    };

    template<class T = double>
    inline constexpr auto real() noexcept(NO_EXCEPT) {
        const auto v = static_cast<T>((this->engine() + 0.5) / (1.0 + this->max()));
        return static_cast<T>((this->operator()() + v) / (1.0 + this->max()));
    }
};


} // namespace uni
Back to top page