Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using random generator with probability using matlab

THe following code generates random numbers with a given probability of success:

         n=[randi([0 1],1,8) ones(1,8)];
         n= n(randperm(10));

If the above lines are repeated random (unpridictable) 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 can I make the generator picking the number of already selected as failure(0) with higher probability?

That is the probability person 2, 3 and 4 in the second round has more probability of loosing. This does not mean that they have to fail.

The entries 1 to 10 are 10 different user outputs.

ok let us say always a max of 30% of entries will be 0. In every time the above is executed. However this is done randomly. So a max of any 3 of the 10 can be zero.

I do not want the probability of success to change.Just to control which one is zero.

To clarify further What I want: If 3 will be chosen "randomly" to be zero then let the previously chosen three have higher probability to be picked and not be picked.

like image 418
pac Avatar asked Mar 20 '26 18:03

pac


1 Answers

Here's a solution with the following logic:

  1. Generate up to 3 failures, assign them randomly
  2. Decide how many failures are in step 2
  3. Give the previous failures a higher chance to fail again

Note that I assume that it is equally likely to have 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
like image 178
Jonas Avatar answered Mar 22 '26 07:03

Jonas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!