在MATLAB中没有得到正确的期望值
我试图使随机数发生器工作,但我的期望是不正确的(除非我从根本上误解了一些东西) 我有这个预设阵列: 数组=[1 0] 我继续使用MATLAB中的rand()函数生成1到3之间的随机索引在MATLAB中没有得到正确的期望值,matlab,statistics,probability,Matlab,Statistics,Probability,我试图使随机数发生器工作,但我的期望是不正确的(除非我从根本上误解了一些东西) 我有这个预设阵列: 数组=[1 0] 我继续使用MATLAB中的rand()函数生成1到3之间的随机索引 index=round(1+2*rand(1,1),0); %generate random index between 1 and 3 随后,我尝试将随机生成的索引访问的条目数组放在一起,进行一定次数的迭代: answer_array=[] %answer holder for i = 1:1:iterati
index=round(1+2*rand(1,1),0); %generate random index between 1 and 3
随后,我尝试将随机生成的索引访问的条目数组放在一起,进行一定次数的迭代:
answer_array=[] %answer holder
for i = 1:1:iterations %Loop for the set number of iterations
index=round(1+2*rand(1,1),0);
answer_array=[answer_array array(index)]
end
然后,我尝试对answer_数组中1的数量求和,并求出其与迭代次数的比率
temp=0;
for i=1:size(answer_array,2)
temp=temp+answer_array(1,i) %just sum all array contents as they're either 1 or 0
end
ratio=temp/iterations
无论我设置的迭代次数有多大,比率都会收敛到0.24。
它不应该是0.33吗?我的理由是,如果我自己随机选择索引,我有1/3的机会得到1。因此,如果我继续大量挑选,我的成功机会将收敛于1/3?尝试此预期值为0.33:
iterations=1000;a=[1 0];r=randi(3,1,迭代);sum(a(r))/iterations
这是有效的。谢谢你知道为什么标准rand不起作用吗?我认为你应该使用floor(rand*3)+1
或ceil(rand*3)
而不是round
你的结果是扭曲的,因为导致索引1
的随机值范围是[1.0,1.5)
。将导致索引2
的值范围是[1.5,2.5)
,或概率0.50
。如果检查索引3
,还应看到概率0.33
。