206 lines
4.2 KiB
C++
206 lines
4.2 KiB
C++
/**
|
||
* @file dl_random.h
|
||
* @brief 随机数
|
||
*
|
||
*
|
||
* @version 1.0
|
||
* @author lveyou
|
||
* @date 23-06-24
|
||
*/
|
||
#pragma once
|
||
|
||
#include <random>
|
||
#include "io/dl_log.h"
|
||
#include "base/dl_type_def.h"
|
||
|
||
namespace dl
|
||
{
|
||
//! 随机数
|
||
class Random
|
||
{
|
||
using FloatType = Float;
|
||
public:
|
||
/**
|
||
* @brief 设置随机数种子
|
||
* @note 0会随机生成一个种子
|
||
*/
|
||
static void SetSeed(uint32_t seed)
|
||
{
|
||
if (seed == 0)
|
||
{
|
||
std::random_device rd;
|
||
_re.seed(_seed = rd());
|
||
}
|
||
else
|
||
_re.seed(_seed = seed);
|
||
}
|
||
/**
|
||
* @brief 返回[min,max)区间的 浮点数随机值
|
||
* @note 如果min等于max,则返回min。如果min大于max则交换位置后比较,但会发出一条警告。
|
||
*/
|
||
template <typename T>
|
||
static T Float(T min, T max)
|
||
{
|
||
//参数检查
|
||
if (min > max)
|
||
{
|
||
log("min > max!");
|
||
std::swap(min, max);
|
||
}
|
||
static std::uniform_real_distribution<T> dist_double;
|
||
//设定区间
|
||
using D = std::decay_t<decltype(dist_double)>;
|
||
using P = typename D::param_type;
|
||
dist_double.param(P{min, max});
|
||
return dist_double(_re);
|
||
}
|
||
|
||
/**
|
||
* @brief 返回[min,max]区间的 随机整数
|
||
* @note 如果min等于max,则返回min。如果min大于max则交换位置后比较,但会发出一条警告。
|
||
*/
|
||
template <typename T>
|
||
static T Integer(T min, T max)
|
||
{
|
||
//参数检查
|
||
if (min > max)
|
||
{
|
||
log("min > max!");
|
||
std::swap(min, max);
|
||
}
|
||
static std::uniform_int_distribution<T> dist_int;
|
||
//设定区间
|
||
using D = std::decay_t<decltype(dist_int)>;
|
||
using P = typename D::param_type;
|
||
dist_int.param(P{min, max});
|
||
return dist_int(_re);
|
||
}
|
||
|
||
/**
|
||
* @brief 返回期望为u,标准差为o的正态分布随机值
|
||
* @note 标准正态分布 1)34% 2)13.5% 3)2.5%
|
||
*/
|
||
template <typename T>
|
||
static T Normal(T u = 0, T o = 1)
|
||
{
|
||
static std::normal_distribution<T> dist;
|
||
//设定区间
|
||
using D = std::decay_t<decltype(dist)>;
|
||
using P = typename D::param_type;
|
||
dist.param(P{ u, o });
|
||
return dist(_re);
|
||
}
|
||
|
||
static FloatType FloatNormal(FloatType u = 0_f, FloatType o = 1_f)
|
||
{
|
||
return Normal(u, o);
|
||
}
|
||
/**
|
||
* @brief 返回标准正态分布的缩放值,且限制到指定值
|
||
*/
|
||
static FloatType FloatNormalX(
|
||
FloatType scale = 1/3_f,
|
||
FloatType min = -1, FloatType max = 1)
|
||
{
|
||
return std::clamp(FloatNormal() * scale, min, max);
|
||
}
|
||
|
||
|
||
static bool Bool()
|
||
{
|
||
return Integer(0, 1) == 1;
|
||
}
|
||
|
||
/**
|
||
* @brief 返回[min,max]区间的 int型整数
|
||
*/
|
||
static int Int(int min, int max)
|
||
{
|
||
return Integer(min, max);
|
||
}
|
||
/**
|
||
* @brief 返回[min,max]区间的 unsigned型整数
|
||
*/
|
||
static unsigned Uint(unsigned min, unsigned max)
|
||
{
|
||
return Integer(min, max);
|
||
}
|
||
/**
|
||
* @brief 返回[min,max]区间的 size_t型整数
|
||
*/
|
||
static size_t SizeT(size_t min, size_t max)
|
||
{
|
||
return Integer(min, max);
|
||
}
|
||
|
||
/**
|
||
* @brief 从容器随机选择一个
|
||
* @param[in] container 容器(不能为空)
|
||
* @retval 返回引用
|
||
*/
|
||
template <typename C>
|
||
static auto& Select(C& container)
|
||
{
|
||
assert(!container.empty());
|
||
size_t index = Integer<size_t>(0, container.size() - 1);
|
||
auto iter = container.begin();
|
||
advance(iter, index);
|
||
return *iter;
|
||
}
|
||
|
||
/**
|
||
* @brief 从容器随机选择一个,并且从容器移除
|
||
* @param[in] container 容器(不能为空)
|
||
* @retval 返回引用
|
||
*/
|
||
template <typename C>
|
||
static auto SelectErase(C& container)
|
||
{
|
||
assert(!container.empty());
|
||
size_t index = Integer<size_t>(0, container.size() - 1);
|
||
auto iter = container.begin();
|
||
advance(iter, index);
|
||
auto ret = *iter;
|
||
container.erase(iter);
|
||
return ret;
|
||
}
|
||
|
||
static Point ExPoint(const Point& p0, const Point& p1)
|
||
{
|
||
return { Int(p0[0], p1[0]), Int(p0[1], p1[1]) };
|
||
}
|
||
static PointU ExPointU(const PointU& p0, const PointU& p1)
|
||
{
|
||
return { Uint(p0[0], p1[0]), Uint(p0[1], p1[1]) };
|
||
}
|
||
static Position ExDirection()
|
||
{
|
||
float r = Random::Angle();
|
||
return { std::cos(r), std::sin(r)};
|
||
}
|
||
/**
|
||
* @brief 返回百分比命中
|
||
*/
|
||
static bool Percent(int v)
|
||
{
|
||
return Int(0, 99) < 50;
|
||
}
|
||
/**
|
||
* @brief 返回角度(弧度制)
|
||
*/
|
||
static float Angle(float min = 0, float max = 3.1415926f * 2.0f)
|
||
{
|
||
return Float(min, max);
|
||
}
|
||
|
||
//! 一般不使用,传给标准库接口才使用
|
||
static std::default_random_engine& GetEngine()
|
||
{
|
||
return _re;
|
||
}
|
||
private:
|
||
static uint32_t _seed;//! 种子值
|
||
static std::default_random_engine _re;//! 随机数序列生成器
|
||
};
|
||
}
|