这是具有以下逻辑的解决方案:
- 最多生成 3 个故障,随机分配它们
- 确定步骤 2 中有多少失败
- 让之前的失败有更高的机会再次失败
请注意,我假设它同样可能有 0、1、2 或 3 次失败。
nRuns = 5;
allRuns = zeros(nRuns,10); %# stores success and failure for all runs
%# (1) generate from 0 to 3 failures (each outcome is equally likely)
nFailures = randi([0 3],1);
tmp = randperm(10);
firstRun = tmp > nFailures
allRuns(1,:) = firstRun;
%# (2) decide how many failures occur in the 2nd run (each outcome is equally likely)
for iRun = 2:nRuns
%# as the loop has been added later
%# I use "2" to indicate any next run
nFailures2 = randi([0 3],1);
%# (3) give previous failures a higher chance to fail again
failIdx = find(~allRuns(iRun-1,:));
successIdx = find(allRuns(iRun-1,:));
%# 5x higher chance of failing for previous failures
failCandidates = [repmat(failIdx,1,5),successIdx];
failCandidatesRand = failCandidates(randperm(length(failCandidates)));
%# if you have R2012a or later
failCandidatesRand = unique(failCandidatesRand ,'stable');
toFail = failCandidatesRand (1:nFailures2);
%# alternatively, if you have R2011b or earlier
toFail = zeros(nFailures2,1);
toFail(1) = failCandidatesRand(1);
ii = 2;
kk = 2;
while ii < (nFailures2+1)
if ~any(toFail==failCandidatesRand(kk));
toFail(ii) = failCandidatesRand(kk);
ii = ii + 1;
end
kk = kk + 1;
end
%# write failures
nextRun= true(1,10);
nextRun(toFail) = false
allRuns(iRun,:) = nextRun;
end