生成正太随机变量

clc
clear
close
format long
syms u v x y u1 u2 u3 u3 u x1 x2 x3 x4 x5 d L
%int(int(int(int(1,x5,x4,1),x4,x3,1),x3,x2,1),x2,x1,1)
%int(int(int(6,x3,x2+d,L),x2,x1+d,L-d),x1,0,L-2*d)
a=3
b=200
y_P=linspace(0,0,a*b);
y_N=linspace(0,0,a*b);
index=0;
t=1000000
for i=1:t
    u1=rand();
    u2=rand();
    v=sqrt(-log(u1)*2)*cos(2*pi*u2);
    
    if v>0
        if v <=a
           index=round(v *b);
           if index == 0
               index=1;
           end
           y_P(index)=y_P(index) +1;
        end
    else    
        if v>=-a
           index=round(abs(v) *b);
           if index == 0
               index=1;
           end
           y_N(index)=y_N(index) +1;
        end
    end
end
% x=[0,0.1,0.2,0.3,0.4]
grid on
hold
 x=linspace(0,a,a*b)
 plot(x,y_P*b/t)
 x=linspace(0,-a,a*b)
 plot(x,y_N*b/t)
View Code

原文地址:https://www.cnblogs.com/wdfrog/p/11205196.html