如何生成具有指定概率的随机数?

时间:2014-07-08 10:52:40

标签: matlab

如何使用特定概率生成1到100之间的随机数矩阵(40x10000)p1 = Prob(1),p2 = Prob(2),...,p100 = Prob(100)?

3 个答案:

答案 0 :(得分:2)

使用权重为randsample

h = 40; w = 10000;
out = reshape( randsample( numel(Prob), h*w, true, Prob ), [h w] );

答案 1 :(得分:1)

如果您没有统计工具箱,则可以手动执行此操作。这样的事情应该有效:

thresholds = cumsum(Prob);
starter = rand(40, 10000);
finalAnswer = starter * 0 + 1; % initialize them all to be 1
for idx = 2:100
  finalAnswer(starter > (thresholds(idx - 1)) & starter <= thresholds(idx)) = idx;
end

答案 2 :(得分:0)

使用统计工具箱,您可以创建具有指定概率的多项分布,然后使用随机生成具有所需大小的样本。

使用概率向量Prob

Pd = makedist('Multinomial','Probabilities',Prob);
out = random(Pd,4,10000);
相关问题