【STD 11】随机数和分布

引擎

引擎是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_distributionInt
uniform_real_distributionReal
贝努利分布bernoulli_distributionBool
binomial_distributionInt
geometric_distributionInt
negative_binomial_distributionInt
泊松分布poisson_distributionInt
exponential_distributionReal
gamma_distributionReal
weibull_distributionReal
extreme_value_distributionReal
正态分布normal_distributionReal
lognormal_distributionReal
chi_squared_distributionReal
cauchy_distributionReal
fisher_f_distributionReal
student_t_distributionReal
抽样分布discrete_distributionInt
piecewise_constant_distributionReal
piecewise_linear_distributionReal

代码

#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;
}

分布数学表达式

  1. 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(it,p)=(ti)pi(1p)ti
  2. geometric distribution
    p ( i ∣ p ) = p ( 1 − p ) i p(i\mid p)=p(1-p)^i p(ip)=p(1p)i
  3. 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(ik,p)=(k+i1i)pk(1p)i
  4. poisson distribution
    p ( i ∣ μ ) = e − μ μ i i ! p(i\mid \mu)=\frac{e^{-\mu}\mu^i}{i!} p(iμ)=i!eμμi
  5. exponential distribution
    p ( x ∣ λ ) = λ e − λ x p(x\mid \lambda)=\lambda e^{-\lambda x} p(xλ)=λeλx
  6. 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α,β)=βαΓ(α)ex/βxα1
  7. 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(xa,b)=ba(bx)a1exp((bx)a)
  8. 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(xa,b)=b1exp(baxexp(bax))
  9. 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)
  10. 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(xm,s)=sx2π 1exp(2s2(lnxm)2)
  11. 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(xn)=Γ(n/2)2n/2x(n/2)1ex/2
  12. 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(xa,b)=(πb(1+(bxa)2))1
  13. 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(xm,n)=Γ(m/2)Γ(n/2)Γ((m+n)/2)(nm)m/2x(m/2)1(1+nmx)(m+n)/2
  14. 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(xn)=nπ 1Γ(n/2)Γ((n+1)/2)(1+nx2)(n+1)/2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Quant0xff

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值