난수생성 클래스/함수

편하게 구성해봤다.

이건 클래스 버전

| 1234567891011121314151617181920212223242526272829 | #include  class Randomer{public:    Randomer() : engine{}, device{}    {        this->engine.seed(device());    }    Randomer(const Randomer&) = delete;    Randomer(Randomer&&) = default;    Randomer& operator=(const Randomer&) = delete;    Randomer& operator=(Randomer&&) = default;    ~Randomer() = default;public:    int pick(int head, int tail)    {        using parm_t = decltype(this->dist)::param_type;        return this->dist(this->engine, parm_t(head, tail));    }    int pick(int end)    {        return this->pick(0, end - 1);    }private:    std::default_random_engine engine;    std::random_device device;    std::uniform_int_distribution dist{};} randomer;Colored by Color Scripter | cs |



이건 전역함수 버전

| 123456789101112131415161718192021222324252627282930313233343536373839 | #include  namespace random{    std::default_random_engine& get_random_engine() //mt39977    {        static std::default_random_engine engine{};        return engine;    }    void randomize() //난수 생성기 장착    {        static std::random_device randomer{};        get_random_engine().seed(randomer());    }    int pick(int head, int tail)     {        static std::uniform_int_distribution dist{};        using parm_t = decltype(dist)::param_type;        return dist(get_random_engine(), parm_t(head, tail));    }    int pick(int end)    {        return pick(0, end - 1);    }    template<typename generator_t>    void randomize(generator_t generator)    {        static std::random_device randomer{};        generator.seed(radomer());    }    template<typename generator_t>    int pick(generator_t generator, int head, int tail)    {        static std::uniform_int_distribution dist{};        //using parm_t = decltype(dist)::param_type;        return dist(getnerator, dist::param_type(head, tail));    }} Colored by Color Scripter | cs |



사용법