# wps生成正态分布的随机数_用matlab生成服从某一分布的随机数

## 1.生成离散型随机变量

### 1.1生成几何分布随机数

1.1.1inverse transform method

，其中
，所以

U = rand(1,1000)
X = floor(log(U)/log(0.6)) + 1
histogram(X)

### 1.2生成泊松分布随机数

X = []
for j = 1 : 1000
i = 0;    lambda = 1;    a = exp(-lambda);    F = a;
for i=1 :1000
if rand(1) < F
x = i;
break;
else
a = lambda * a / (i + 1);
F = F + a;
end
end
X = [X, x]
end
histogram(X)

### 1.3生成二项分布随机数

n = 1000;
p = 0.4;c = p / (1-p);X = [];
for j = i : 1000
i = 0;    a = (1-p)^n;    F = a;
for i = i : 1000
if rand(1) < F
x = i;    break;
else
a = c * a * (n-i) / (i + 1);    F = F + a;
end
end
X = [X, x];
end
histogram(X)

## 2.1Inverse Transform Algorithm：

，那么有：

### 2.1.1生成指数分布随机数

lam = 2;
uni = rand(1, 1000);
X = -log(uni) / lam;
histogram(X)

### 2.2the rejection method

，考虑
，接下来找一个最小的c, s.t.
。因此，只需求

X = [];
for j = i : 1000
for i = 1 : 1000
U1 = rand(1);   U2 = rand(1);
if U2 <= 256 * U1 * (1 - U1)^3 / 27
x = U1;
break;
end
end
X = [X, x];
end
histogram(X)

### 2.3生成正态分布随机数

Z = [];
for j = 1 : 1000
for i = 1 : 1000
y1 = -log(rand(1));
y2 = -log(rand(1));
if y2 > 0.5 * (y1 - 1)^2
y = 0.5 * (y1 - 1)^2;
break;
end
end
U = rand(1);
if U <= 0.5
z = y1;
else
z = -y1;
end
Z = [Z, z];
end
histogram(Z)
10-18

03-07
07-23 1万+
05-03 418
08-25 196
11-02 1万+
04-22 4060
02-07 4594
01-15 43
©️2020 CSDN 皮肤主题: 1024 设计师:白松林