Using a random generator with probability using matlab
The following code generates random numbers with a given success rate:
n=[randi([0 1],1,8) ones(1,8)];
n= n(randperm(10));
If the above lines are repeated, random (unmanaged) values will be generated:
This is n for the first run:
n = 1 0 1 0 0 1 1 1 1 1
This is n for the second run:
n = 1 1 1 0 1 1 1 1 1 1
How do I make the generator choose the number of those already selected as failure (0) with a greater probability?
This is the probability that person 2, 3, and 4 in the second round are more likely to be lost. This does not mean that they should fail.
Entries 1 through 10 represent 10 different user outputs.
ok, let's say always a maximum of 30% of the records will be 0. At every moment of execution above. However, this is done randomly. So the maximum of any 3 out of 10 can be zero.
I don't want the probability of success to change. Just to determine which one is zero.
To clarify what I want: if 3 is chosen "randomly" equal to zero, then let the previously selected three have a higher probability of being selected and will not be chosen.
source to share
There is a solution here with the following logic:
- Create up to 3 crashes, randomly assign them
- Determine the number of failures in step 2
- Give previous bugs a higher chance of crashing again.
Please note that I am assuming it has an equal chance of having 0,1,2 or 3 failures.
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
source to share
Perhaps you want something like this:
n1 = rand(1, 8) > 0.5;
n2 = rand(1, 8) > (n1 * 0.5 + ~n1 * 0.6);
This would generate the first set of 0s and 1s with equal probability. For n2, if the corresponding value in n1 is 1, then there is a 50% probability of 0 or 1. If the corresponding value in n1 = 0, then there is a probability of 60% 0.
Thanks for clarifying the problem. Try again! I think you can use randsample for this. This is a bit of a mess because there seems to be no way for weighted sampling without replacement.
N = ones(1, 10);
for round=1:10
failures = randi([1, 3]);
indices = [];
while (numel(unique(indices)) < failures)
indices = randsample(10, failures, true, [N + ~N*1.1]);
end
N = ones(1, 10);
N(indices) = 0;
disp(N);
end
source to share