1 Star 0 Fork 0

陈祎麒/mlcc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Random.h 1.57 KB
一键复制 编辑 原始数据 按行查看 历史
sb500 提交于 2020-05-10 13:24 . 加上一个初始化一片
#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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/chen-yiqi-duke/mlcc.git
git@gitee.com:chen-yiqi-duke/mlcc.git
chen-yiqi-duke
mlcc
mlcc
master

搜索帮助