Created
October 5, 2019 12:10
-
-
Save lixingcong/c3cb51ebc14721d4459ddac417525ac3 to your computer and use it in GitHub Desktop.
生成随机数C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MYRANDOM_HPP | |
#define MYRANDOM_HPP | |
// 源码:https://evileg.com/en/post/306 | |
#include <random> | |
namespace details | |
{ | |
/// True if type T is applicable by a std::uniform_int_distribution | |
template<class T> | |
struct is_uniform_int | |
{ | |
static constexpr bool value = std::is_same<T, short>::value || std::is_same<T, int>::value || std::is_same<T, long>::value || std::is_same<T, long long>::value | |
|| std::is_same<T, unsigned short>::value || std::is_same<T, unsigned int>::value || std::is_same<T, unsigned long>::value | |
|| std::is_same<T, unsigned long long>::value; | |
}; | |
/// True if type T is applicable by a std::uniform_real_distribution | |
template<class T> | |
struct is_uniform_real | |
{ | |
static constexpr bool value = std::is_same<T, float>::value || std::is_same<T, double>::value || std::is_same<T, long double>::value; | |
}; | |
} // namespace details | |
class Random | |
{ | |
template<class T> | |
using IntDist = std::uniform_int_distribution<T>; | |
template<class T> | |
using RealDist = std::uniform_real_distribution<T>; | |
public: | |
template<class T> | |
static typename std::enable_if<details::is_uniform_int<T>::value, T>::type get(T from = std::numeric_limits<T>::min(), T to = std::numeric_limits<T>::max()) | |
{ | |
if (from > to) | |
std::swap(from, to); | |
IntDist<T> dist{from, to}; | |
return dist(instance().engine()); | |
} | |
template<class T> | |
static typename std::enable_if<details::is_uniform_real<T>::value, T>::type get(T from = std::numeric_limits<T>::min(), T to = std::numeric_limits<T>::max()) | |
{ | |
if (from > to) | |
std::swap(from, to); | |
RealDist<T> dist{from, to}; | |
return dist(instance().engine()); | |
} | |
std::mt19937& engine() { return m_mt; } | |
protected: | |
static Random& instance() | |
{ | |
static Random inst; | |
return inst; | |
} | |
private: | |
std::random_device m_rd; // Random Number Generator | |
std::mt19937 m_mt; // Standard random number generator | |
Random() | |
: m_mt(m_rd()) | |
{} | |
~Random() {} | |
Random(const Random&) = delete; | |
Random& operator=(const Random&) = delete; | |
}; | |
#endif // MYRANDOM_HPP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment