【问题标题】:How to generate random events based on a given probability? [closed]如何根据给定的概率生成随机事件? [关闭]
【发布时间】:2015-07-31 14:10:40
【问题描述】:
我需要在某个组件的整个生命周期内产生一定数量的随机故障。我拥有组件的故障率,即如果我知道它在 720 小时的工作中失败了 6 次,那么它的故障率为 6/720 = 0.0083 次故障/小时。
现在,如果我考虑两种可能的运行状态(0 = 组件工作正常,1 = 组件失败),我想在 Matlab 中创建一个脚本,该脚本为我提供一个数组,该数组用于总生命周期的 720 小时中的每一个根据已知的故障率,它给我一个 0 或 1,无论组件是否工作。非常感谢。
【问题讨论】:
-
解决方案很大程度上取决于对世界如何运作的假设。例如。如果一个组件发生故障,它会一直处于故障状态,它是否可以返回到功能状态,或者它是否被替换为相同的组件?故障事件的分布是怎样的?另见:survival analysis
-
标签:
matlab
statistics
probability
random-sample
reliability
【解决方案1】:
众多解决方案之一:
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)
【解决方案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