如何根据给定的概率生成随机事件?

时间:2015-05-19 16:12:34

标签: matlab statistics probability random-sample reliability

我需要在某个组件的整个生命周期中生成一定数量的随机故障。我拥有组件的故障率,即如果我知道它在720小时的工作中失败了6次,那么它的故障率是6/720 = 0.0083次故障/小时。 现在,如果我考虑两种可能的运行状态(0 =组件工作正常,1 =组件失败),我想在Matlab中创建一个脚本,为我提供一个数组,该数组在整个生命周期的720小时中的每一小时根据已知的故障率,它给出了0或1,组件是否正常工作。非常感谢。

2 个答案:

答案 0 :(得分:2)

另一个例子

numFailures = 6;
timeLength = 720; % hours

pFailure = numFailures / timeLength;

% call this to randomly determine if there was a failure. If called enough times the probability of 1 will be equal to pFailure
isFailed = rand(1) < pFailure;

我们可以通过循环调用来验证:

for k=1:1e5
     isFailed(k) = rand(1) < pFailure;
end



sum(isFailed)/k

ans =

   0.008370000000000

答案 1 :(得分:0)

众多解决方案之一:

numFailures = 6;
timeLength = 720; % hours

% create array with one element per hour
p = false(1, timeLength);
p(1:numFailures) = true;

% randomly sort p
p = p(randperm(timeLength));

% generate a random index for sampling
index = randi(timeLength, 1);

% Sample the array
% this will be true if there was a failure
p(index)