代码拉取完成,页面将自动刷新
#pragma once
#include <random>
enum RandomType
{
RANDOM_UNIFORM = 0,
RANDOM_NORMAL = 1,
};
template <typename T = float>
class Random
{
protected:
RandomType type_ = RANDOM_UNIFORM;
std::random_device device_;
std::mt19937 generator_;
std::uniform_real_distribution<T> uniform_dist_{ 0, 1 };
std::normal_distribution<T> normal_dist_{ 0, 1 };
std::minstd_rand0 generator_fast_;
public:
Random() { set_seed(); }
void set_random_type(RandomType t) { type_ = t; }
void set_parameter(T a, T b)
{
uniform_dist_.param(decltype(uniform_dist_.param())(a, b));
normal_dist_.param(decltype(normal_dist_.param())(a, b));
}
T rand()
{
if (type_ == RANDOM_UNIFORM)
{
return uniform_dist_(generator_);
}
else if (type_ == RANDOM_NORMAL)
{
return normal_dist_(generator_);
}
return 0;
}
void set_seed()
{
generator_ = std::mt19937(device_());
}
void set_seed(unsigned int seed)
{
generator_ = std::mt19937(seed);
}
int rand_int(int n)
{
return int(rand() * n);
}
int rand_int(int n1, int n2)
{
return n1 + int(rand() * (n2 - n1));
}
std::mt19937& get_generator()
{
return generator_;
}
void rand_data(T* data, size_t size)
{
for (int i = 0; i < size; i++)
{
data[i] = rand();
}
}
};
using RandomDouble = Random<double>; //use this in usual
using RandomFloat = Random<float>; //use this in usual
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。