2015-05-19 55 views
1

我需要在某個組件的整個生命週期內生成一定數量的隨機故障。我擁有組件的故障率,即如果我知道它在720小時的工作中失敗了6次,那麼它的故障率是6/720 = 0.0083次/小時。 現在,如果我考慮兩種可能的功能狀態(0 =組件工作正常,1 =組件失敗),我想在Matlab中創建一個腳本,爲我提供一個數組,該數組對於720小時生命週期,它會根據已知的故障率給出0或1,無論組件是否工作。非常感謝。如何根據給定的概率生成隨機事件?

+1

該解決方案的假設很強烈依賴有關如何世界的作品。例如。如果某個組件發生故障,它是否保持失敗,是否可以返回到功能狀態,還是由相同的組件替換?失敗事件的分佈是什麼?另見:[生存分析](http://en.wikipedia.org/wiki/Survival_analysis) –

+0

既然你提到「每個小時」,我想你想考慮離散時間? –

回答

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) 
+1

將下襬放在相同的答案! –

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