能不能帮忙解决个问题?
楼主 · 1 楼
是个概率的问题,抛硬币的出正反面的概率是50%,我能模拟出50%的随机正反面的值并记入数据库,如果设定抛正反面的概率分别是30%和70%,能否给一个方法把,把抛硬币正反面的概率分别是30%和70%的记录记下再存入数据库中,谢谢帮忙
2 楼
function out=drnd(p,n)
%p is the probability distribution matrix;
%n is the number of the samples you want to generate;
%=================a=cumsum(p(2,:));
b=rand(n,1);
out=zeros(n,1);
for k=1:n
c=find( a<b(k) );
if ( isempty ( c ) )
out(k)=p(1,1);
else
out(k)=p(1,c(end)+1);
end
end
我编写的MATLAB产生 离 散 分 布 随 机 数 的 程 序.?p为分布矩阵.例如本题令出现正面为"1",反面为"0",则p=[0 1; 0.7 0.3 ];
模拟100次结果如下:
out=drnd([1 0;0.3 0.7],100);
out'
ans = Columns 1 through 19
0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0
Columns 20 through 38
1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1
Columns 39 through 57
0 0 0 1 0 0 1 1 0 0 0 0 0 1 1 0 1 0 0
Columns 58 through 76
1 1 0 0 1 1 1 0 1 0 0 0 0 1 0 0 0 1 0
Columns 77 through 95
0 0 0 0 1 0 0 0 1 1 0 1 0 0 1 1 1 1 0
Columns 96 through 100
1 0 1 1 0
%p is the probability distribution matrix;
%n is the number of the samples you want to generate;
%=================a=cumsum(p(2,:));
b=rand(n,1);
out=zeros(n,1);
for k=1:n
c=find( a<b(k) );
if ( isempty ( c ) )
out(k)=p(1,1);
else
out(k)=p(1,c(end)+1);
end
end
我编写的MATLAB产生 离 散 分 布 随 机 数 的 程 序.?p为分布矩阵.例如本题令出现正面为"1",反面为"0",则p=[0 1; 0.7 0.3 ];
模拟100次结果如下:
out=drnd([1 0;0.3 0.7],100);
out'
ans = Columns 1 through 19
0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0
Columns 20 through 38
1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1
Columns 39 through 57
0 0 0 1 0 0 1 1 0 0 0 0 0 1 1 0 1 0 0
Columns 58 through 76
1 1 0 0 1 1 1 0 1 0 0 0 0 1 0 0 0 1 0
Columns 77 through 95
0 0 0 0 1 0 0 0 1 1 0 1 0 0 1 1 1 1 0
Columns 96 through 100
1 0 1 1 0