pac*_*pac 4 matlab probability
我使用以下代码
operation=[rand(1,noOfNodes)>prob];
Run Code Online (Sandbox Code Playgroud)
生成1和0(noOfNodes次).如果我使用prob=0.2并尝试100个值,则在某些情况下存在40个零.这不奇怪吗?
我需要获得小于0.2的零的概率
不,那不奇怪.这是你的概率.
如果你翻硬币100次,你不会总是得到50个头和50个尾巴.有时你得到49和51,在最稀有的场合,你甚至可以得到相同的100次.
有了您上面的代码你不能保证总能得到20个零和80组的人时noOfNodes是100.如果你想生成一个矢量总是有20%的零,但随机排序 0和1,那么你就可以做到这一点像这样使用RANDPERM功能:
operation = [zeros(1,20) ones(1,80)]; %# Fill the vector with 0 and 1
operation = operation(randperm(100)); %# Randomly reorder it
Run Code Online (Sandbox Code Playgroud)
如果要生成具有0%到20%零的向量,可以使用函数RANDI修改上面的代码:
operation = [randi([0 1],1,20) ones(1,80)];
operation = operation(randperm(100));
Run Code Online (Sandbox Code Playgroud)