Ai
1 Star 0 Fork 0

green-gitee/博客园

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
std_random.hxx 2.61 KB
一键复制 编辑 原始数据 按行查看 历史
green-gitee 提交于 2024-07-23 22:09 +08:00 . Added resources of 18318363.
// std_random.hxx
#ifndef _BLOGS_STD_RANDOM
#define _BLOGS_STD_RANDOM
#include <random>
#include "array.hxx"
namespace blogs
{
using std::knuth_b;
using std::uniform_real_distribution;
class Std_Random
{
private:
static
knuth_b
__Engine__;
static
uniform_real_distribution<double>
__Distri__;
public:
/*
* Set random seed.
*
*/
static
void
set_seed(long);
/*
* Generate a real ranging [0, 1).
*
*/
static
double
random();
/*
* Generate an integer ranging [0, N).
*
*/
static
int
uniform(int);
/*
* Generate an integer ranging [lo, hi).
*
*/
static
int
uniform(int, int);
/*
* Generate a long integer ranging [0, N).
*
*/
static
long
uniform(long);
/*
* Generate a long integer ranging [lo, hi-1).
*
*/
static
long
uniform(long, long);
/*
* Generate a double ranging [lo, hi).
*
*/
static
double
uniform(double, double);
/*
* Generate a random boolean from a Bernoulli distribution.
*
*/
static
bool
bernoulli(double = 0.5);
/*
* Generate a random real number from a Gaussian distribution,
* with mean and standard deviation.
*
*/
static
double
gaussian(double = 0.0, double = 1.0);
/*
* Generate a random integer from a Poisson distribution,
* with the mean of lambda.
*
*/
static
int
poisson(double = 0.5);
/*
* Generate a random integer from a Geometric distribution,
* with the success probability.
*
*/
static
int
geometric(double = 0.5);
/*
* Generate a random integer from a Discrete distribution,
* with the probabilities.
*
*/
static
int
discrete(Array<double> const&);
/*
* Shuffle the array.
*
*/
template <class _T>
static
void
shuffle(Array<_T> & a)
{
int n = a.size(), r;
for (int i = 0; i < n; ++i) {
r = i + uniform(long(n-i)); // between i and n-1
_T t = a[i];
a[i] = a[r];
a[r] = t;
}
}
/*
* Shuffle the array.
*
*/
template <class _T>
static
void
shuffle(Array<_T> & a, int b, int e)
{
int r;
for (int i = b; i < e; ++i) {
r = i + uniform(long(e-i)); // between i and e-1
_T t = a[i];
a[i] = a[r];
a[r] = t;
}
}
};
} // end of namespace blogs
#endif
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/green-gitee/cnblogs.git
git@gitee.com:green-gitee/cnblogs.git
green-gitee
cnblogs
博客园
master

搜索帮助