📄 -
字号:
用round(19*rand+2)产生的随机数不均匀,应用fix(20*rand+2)或round(20*rand+1.5)产生随机数
有以下检测可知三者中round(20*rand+1.5)最好。
***********************
检测程序:
clear
clc
m2to21=zeros(1,20);
n2to21=zeros(1,20);
k2to21=zeros(1,20);
normal=1/20
for i=1:10000000
m1=round(19*rand+2);
m2=round(20*rand+1.5);
n=fix(20*rand+2);
for j=2:21
if j==m1
m2to21(j-1)=m2to21(j-1)+1;
end
if j==n
n2to21(j-1)=n2to21(j-1)+1;
end
if j==m2
k2to21(j-1)=k2to21(j-1)+1;
end
end
end
Pm1=m2to21/10000000
Pm2=k2to21/10000000
Pn=n2to21/10000000
************************
结果:
normal =
0.0500
Pm1 =
Columns 1 through 6
0.0263 0.0526 0.0526 0.0527 0.0527 0.0526
Columns 7 through 12
0.0527 0.0527 0.0526 0.0527 0.0527 0.0526
Columns 13 through 18
0.0527 0.0525 0.0526 0.0526 0.0527 0.0527
Columns 19 through 20
0.0525 0.0263
******** 2和21的概率是其他的一半 ******** 错误
Pm2 =
Columns 1 through 6
0.0500 0.0501 0.0499 0.0500 0.0501 0.0500
Columns 7 through 12
0.0501 0.0500 0.0501 0.0499 0.0500 0.0500
Columns 13 through 18
0.0500 0.0499 0.0500 0.0500 0.0501 0.0499
Columns 19 through 20
0.0500 0.0500
********** 出现4次0.0499 *********** 合适
Pn =
Columns 1 through 6
0.0499 0.0500 0.0500 0.0500 0.0499 0.0500
Columns 7 through 12
0.0500 0.0500 0.0499 0.0500 0.0501 0.0501
Columns 13 through 18
0.0501 0.0500 0.0499 0.0501 0.0498 0.0500
Columns 19 through 20
0.0500 0.0500
*********** 出现4次0.0499和一次0.0498 *********** 合适
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -