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

  • Home
  • SEARCH
  • 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 399153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:49:06+00:00 2026-05-12T16:49:06+00:00

I saw this code in a comment for the article Never-ending Shuffled Sequence .

  • 0

I saw this code in a comment for the article “Never-ending Shuffled Sequence“. I understand the basic premise, but I don’t know how it works. The biggest explanation I need is of the first two lines of the while loop.

(Because it is written in MATLAB I can only guess at how this code functions.)

probabilities = [1 1 1 1 1 1];
unrandomness = 1;
while true
    cumprob = cumsum(probabilities) ./ sum(probabilities);
    roll = find(cumprob >= rand, 1)
    probabilities = probabilities + unrandomness;
    probabilities(roll) = probabilities(roll) - 6*unrandomness;
    if min(probabilities) < 0
        probabilities = probabilities - min(probabilities);
    end
end
  • 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-05-12T16:49:07+00:00Added an answer on May 12, 2026 at 4:49 pm

    The probabilities vector represents the relative weights for the likelihood that the numbers 1 through 6 will be selected. At the start, they all have an equal chance of being picked. I’ll step through each line of the while loop explaining what it does:

    • The first line within the while loop creates a cumulative probability from the probabilities vector. The CUMSUM function is used to return a cumulative sum along the length of the vector, and this is divided by the total sum of the vector (found using the SUM function). On the first pass through the loop, cumprob will have these values:

      0.1667    0.3333    0.5000    0.6667    0.8333    1.0000
      

      Notice that these create “bins” that a random number from 0 to 1 can fall in. The probability that a number will fall within a given bin is equal to the width of that bin, so there’s a 1 in 6 (0.1667) chance that a randomly drawn number will fall in the first bin (from 0 to 0.1667), or the second bin (from 0.1667 to 0.3333), etc..

    • The second line within the while loop picks a random number (using the RAND function) and finds the index of the first element in cumprob that is larger than that value (using the FIND function). The roll value is thus a number from 1 to 6.

    • The third line within the while loop adds “unrandomness” by shifting all the relative weights upward, moving the probabilities a little closer to being equal for all the numbers. Consider the example where probabilities has the following form:

      [x x x 1 x x]
      

      where x is some value greater than 1. At this point, the probability that the value 4 is chosen is 1/(5*x+1). By adding 1 to all the elements, that probability becomes 2/(5*x+7). For x = 3, the probability of 4 occurring increases from 0.0625 to 0.0909, while the probability of any other number occurring decreases from 0.1875 to 0.1818. This “unrandomness” is thus acting to normalize probabilities.

    • The fourth line within the while loop essentially does the opposite of the previous line by significantly dropping the relative weight of whatever number just occurred, making it less likely to happen on subsequent loops. This reduced likelihood of occurrence will be short lived due to the effect of the previous line constantly trying to bring the probabilities of occurrence back to equal for all the numbers.

      Note that the amount subtracted from the one element of probabilities is equal to the total amount added to all the elements in the previous line, resulting in a net change of zero for the total sum of the probabilities vector. This keeps the values in probabilities bounded so that they don’t just keep growing and growing.

    • The if statement at the end of the while loop is simply there to make sure all the numbers in probabilities are positive. If the minimum value of the vector (found using the MIN function) is less than zero, then this value is subtracted from every element of the vector. This will make sure the cumprob vector always has values between 0 and 1.

    If you replace the while true statement with for i = 1:6, display the probabilities vector and roll value at the end of each iteration, and run the code a few times over you can see how the code does what it does. Here’s one such set of 6 rolls that draws each of the numbers 1 through 6 once:

    roll             probabilities
    
     5   |  6     6     6     6     0     6
         |
     4   |  7     7     7     1     1     7
         |
     2   |  8     2     8     2     2     8
         |
     1   |  3     3     9     3     3     9
         |
     3   |  4     4     4     4     4    10
         |
     6   |  5     5     5     5     5     5
    

    Notice how the final values in probabilities are all equal, meaning that at that point the numbers 1 through 6 all have an equal likelihood of being chosen once again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 231k
  • Answers 231k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the following function like this: Image('/path/to/original.image', '1/1', '150*', './thumb.jpg');… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer Check you database schema to see if the field (referenced… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer I figured out the problem - there was a session… May 13, 2026 at 2:13 am

Related Questions

I have a property Foo on a class Bar: public int Foo { get
I've got a LOT of tests written for a piece of software (which is
I have some questions about the following code: using System; namespace ConsoleApplication2 { public
We have a sub-project 'commonUtils' that has many generic code-snippets used across the parent

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.