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我的朋友怎麼樣?我沒有得到點 –