引擎
引擎是function object,可以产生随机的无正负值.
基础引擎
以下三种基础引擎提供基本算法用以生成随机值.
std::linear_congruential_engine
std::measure_twister_engine
std::substract_with_carry_engine
引擎适配器
可以根据基础引擎进行初始化
std::discard_block_engine
,每次抛弃给定数量的生成值
std::independent_bits_engine
,以一个具体的指定数量bit
生成随机值
std::shuffle_order_engine
,将生成值的次序加以排列组合
代码
#include<iostream>
#include<vector>
#include<cmath>
#include<cstring>
#include<cassert>
using namespace std;
int main(){
default_random_engine dre; // 设置随机数引擎
uniform_int_distribution<int> di(10, 20); // 均匀分布, int
for(int i=0; i<20; ++i) cout<<di(dre)<<" ";
cout<<endl;
uniform_real_distribution<double> dr(10, 20); // 均匀分布, float
for(int i=0; i<10; ++i) cout<<dr(dre)<<" ";
cout<<endl;
vector<int> v={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
shuffle(v.begin(), v.end(), dre); // 洗牌函数
for(auto &e: v) cout<<e<<" ";
cout<<endl;
return 0;
}
分布
分类 | 函数名 | 数据类型 |
---|---|---|
均匀分布 | uniform_int_distribution | Int |
uniform_real_distribution | Real | |
贝努利分布 | bernoulli_distribution | Bool |
binomial_distribution | Int | |
geometric_distribution | Int | |
negative_binomial_distribution | Int | |
泊松分布 | poisson_distribution | Int |
exponential_distribution | Real | |
gamma_distribution | Real | |
weibull_distribution | Real | |
extreme_value_distribution | Real | |
正态分布 | normal_distribution | Real |
lognormal_distribution | Real | |
chi_squared_distribution | Real | |
cauchy_distribution | Real | |
fisher_f_distribution | Real | |
student_t_distribution | Real | |
抽样分布 | discrete_distribution | Int |
piecewise_constant_distribution | Real | |
piecewise_linear_distribution | Real |
代码
#include<iostream>
#include<vector>
#include<cmath>
#include<cstring>
#include<cassert>
#include<iomanip>
#include<random>
#include<map>
typedef long long LL;
using namespace std;
const int CNT=100005;
template<typename Distr, typename Eng>
void distr(Distr d, Eng e, const string &name){
cout<<name<<":"<<endl;
cout<<"-lower bound: "<<d.min()<<endl;
cout<<"-upper bound: "<<d.max()<<endl;
cout<<"-values: "<<d(e)<<" "<<d(e)<<" "<<d(e)<<endl;
map<LL, int> mp;
for(int i=0; i<CNT; ++i) mp[d(e)]++;
cout<<"------------------------"<<endl;
for(auto &e: mp){
cout<<setw(3)<<e.first<<": "<<e.second<<endl;
}
cout<<"------------------------"<<endl;
cout<<endl;
}
int main(){
knuth_b e; // 设置随机数引擎
uniform_real_distribution<> ud(0, 21); // 均匀分布
distr(ud, e, "uniform real distribution");
normal_distribution<> nd; // 正态分布
distr(nd, e, "normal distribution");
exponential_distribution<> ed; // 指数分布
distr(ed, e, "exponential distribution");
gamma_distribution<> gd; // gamma分布
distr(gd, e, "gamma distribution");
return 0;
}
分布数学表达式
- binomial distribution
p ( i ∣ t , p ) = ( t i ) p i ( 1 − p ) t − i p(i\mid t, p)=\left( \begin{matrix} t\\ i \end{matrix} \right)p^i(1-p)^{t-i} p(i∣t,p)=(ti)pi(1−p)t−i - geometric distribution
p ( i ∣ p ) = p ( 1 − p ) i p(i\mid p)=p(1-p)^i p(i∣p)=p(1−p)i - negative binomial distribution
p ( i ∣ k , p ) = ( k + i − 1 i ) p k ( 1 − p ) i p(i\mid k, p)=\left( \begin{matrix} k+i-1\\ i \end{matrix} \right)p^k(1-p)^i p(i∣k,p)=(k+i−1i)pk(1−p)i - poisson distribution
p ( i ∣ μ ) = e − μ μ i i ! p(i\mid \mu)=\frac{e^{-\mu}\mu^i}{i!} p(i∣μ)=i!e−μμi - exponential distribution
p ( x ∣ λ ) = λ e − λ x p(x\mid \lambda)=\lambda e^{-\lambda x} p(x∣λ)=λe−λx - gamma distribution
p ( x ∣ α , β ) = e − x / β β α Γ ( α ) x α − 1 p(x\mid \alpha, \beta)=\frac{e^{-x/\beta}}{\beta^\alpha\Gamma(\alpha)}x^{\alpha-1} p(x∣α,β)=βαΓ(α)e−x/βxα−1 - weibull distribution
p ( x ∣ a , b ) = a b ( x b ) a − 1 exp ( − ( x b ) a ) p(x\mid a, b)=\frac{a}{b}(\frac{x}{b})^{a-1}\exp(-(\frac{x}{b})^a) p(x∣a,b)=ba(bx)a−1exp(−(bx)a) - extreme value distribution
p ( x ∣ a , b ) = 1 b exp ( a − x b − exp ( a − x b ) ) p(x\mid a, b)=\frac{1}{b}\exp(\frac{a-x}{b}-\exp(\frac{a-x}{b})) p(x∣a,b)=b1exp(ba−x−exp(ba−x)) - normal distribution
p ( x ∣ μ , σ ) = 1 σ 2 π exp ( − ( x − μ ) 2 2 σ 2 ) p(x\mid \mu, \sigma)=\frac{1}{\sigma\sqrt{2\pi}}\exp(-\frac{(x-\mu)^2}{2\sigma^2}) p(x∣μ,σ)=σ2π1exp(−2σ2(x−μ)2) - lognormal distribution
p ( x ∣ m , s ) = 1 s x 2 π exp ( − ( ln x − m ) 2 2 s 2 ) p(x\mid m, s)=\frac{1}{sx\sqrt{2\pi}}\exp(-\frac{(\ln x-m)^2}{2s^2}) p(x∣m,s)=sx2π1exp(−2s2(lnx−m)2) - chi squared distribution
p ( x ∣ n ) = x ( n / 2 ) − 1 e − x / 2 Γ ( n / 2 ) 2 n / 2 p(x\mid n)=\frac{x^{(n/2)-1}e^{-x/2}}{\Gamma(n/2)2^{n/2}} p(x∣n)=Γ(n/2)2n/2x(n/2)−1e−x/2 - cauchy distribution
p ( x ∣ a , b ) = ( π b ( 1 + ( x − a b ) 2 ) ) − 1 p(x\mid a, b)=(\pi b(1+(\frac{x-a}{b})^2))^{-1} p(x∣a,b)=(πb(1+(bx−a)2))−1 - fisher f distribution
p ( x ∣ m , n ) = Γ ( ( m + n ) / 2 ) Γ ( m / 2 ) Γ ( n / 2 ) ( m n ) m / 2 x ( m / 2 ) − 1 ( 1 + m x n ) − ( m + n ) / 2 p(x\mid m, n)=\frac{\Gamma((m+n)/2)}{\Gamma(m/2)\Gamma(n/2)}(\frac{m}{n})^{m/2}x^{(m/2)-1}(1+\frac{mx}{n})^{-(m+n)/2} p(x∣m,n)=Γ(m/2)Γ(n/2)Γ((m+n)/2)(nm)m/2x(m/2)−1(1+nmx)−(m+n)/2 - student t distribution
p ( x ∣ n ) = 1 n π Γ ( ( n + 1 ) / 2 ) Γ ( n / 2 ) ( 1 + x 2 n ) − ( n + 1 ) / 2 p(x\mid n)=\frac{1}{\sqrt{n\pi}}\frac{\Gamma((n+1)/2)}{\Gamma{(n/2)}}(1+\frac{x^2}{n})^{-(n+1)/2} p(x∣n)=nπ1Γ(n/2)Γ((n+1)/2)(1+nx2)−(n+1)/2