[matlab]一种生成正态分布随机数的方法

一种生成正态随机数的方法

生成步骤

  • 第一步: 产生两个(0,1)上独立的均匀分布变量u1和u2
  • 第二步:考虑如下两个变量
    X1=(2loge(u1))1/2cos(2πu2) X 1 = ( − 2 log e ⁡ ( u 1 ) ) 1 / 2 cos ⁡ ( 2 π u 2 )

    X2=(2loge(u1))1/2sin(2πu2) X 2 = ( − 2 log e ⁡ ( u 1 ) ) 1 / 2 sin ⁡ ( 2 π u 2 )
  • 第三步:则(X1, X2)是一对独立同分布的正态分布随机变量,都服从 N(0,1) N ( 0 , 1 ) 分布

注:理论证明请参考文献《A Note on the Generation of Random Normal Deviates

示例代码

function x=normal(size)
%input: the size of random variables
%output:the sequence of random variable that obey normal distribution
x=zeros(1,2*size);
for i=1:size
    u=unifrnd(0,1,1,2); %generate two independent random variables with uniform distribution on (0,1)
   %% generate X1 and X2 
    X1=sqrt(-2*log(u(1)))*cos(2*pi*u(2));
    X2=sqrt(-2*log(u(1)))*sin(2*pi*u(2));
    x(2*i-1)=X1; x(2*i)=X2;
end
%evaluate the mean of sequence x
mean_of_x=mean(x)
%evaluate the variance of sequence x
var_of_x=var(x)
%draw histogram
hist(x,1000);

运行结果

>> x=normal(10e5);

mean_of_x =

   1.0443e-04


var_of_x =

    0.9999

这里写图片描述

  • 7
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值