0
我看到下面的问题一个一个站点:生成0和1,用25%和75概率
how to generate 0 and 1 with 25% and 75% probability, here is corresponding code in c++
int rand50()
{
// rand() function will generate odd or even
// number with equal probability. If rand()
// generates odd number, the function will
// return 1 else it will return 0.
return rand() & 1;
}
bool rand75()
{
return rand50() | rand50();
}
但不幸的是我不能这样做
>> bitand(rand,1)
Error using bitand
Double inputs must have integer values in the range of ASSUMEDTYPE.
因此我用以下算法 1. rand50
function boolean=rand50()
probability=rand;
if probability <=0.5
boolean=0;
else
boolean=1;
end
end
2.rand75
function boolean=rand75()
boolean=bitor(rand50(),rand50());
end
我认为它们是等价的正确?或至少很好地近似在C/C而在MATLAB它返回0
和1
之间的double
++返回一个整数
输出的FUNC改变“50”输入变量的名称将允许你产生以任意频率分配的0/1,例如, 25%/ 75% – Jonas
非常感谢主席先生\ –
@JOnas我的朋友怎么样?我没有得到点 –