Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7771649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:42:14+00:00 2026-06-01T16:42:14+00:00

THe following code generates random numbers with a given probability of success: n=[randi([0 1],1,8)

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:42:16+00:00Added an answer on June 1, 2026 at 4:42 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code to generate two random numbers var attackRoll = Math.floor((Math.random()*6)+1);
I have the following code (generates a quadratic function given the a, b, and
Possible Duplicate: Generating random numbers in Javascript I have the following code var randomnumber=Math.floor(Math.random()*101);
The following code generates random number from 1 to 10 $ran = rand(1,10); What
The following code generates a FileNotFoundException (using .NET 2.0): using System; using System.Collections.Generic; using
I don't understand why the following code generates a warning. interface Generic<T> { }
The following code snippet generates some warning messages when compiling: Cluster& Myclass::getCluster(const Point &p)
Class 1 has the following code that generates the exception - -(IBAction) searchAllAction: (id)
The following line of code generates the compile time error (PE19) There is no
One of the following pieces of code generates a memory leak, any idea which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.