How does this MATLAB code work? (probabilities and random sequences)

I saw this code in a comment for the article " Endless shuffled sequence ." I understand the basic premise, but I do not know how this works. The biggest explanation I need is the first two lines of the while loop.

(Since it is written in MATLAB, I can only guess how this code works.)

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 
+4
source share
1 answer

The probabilities vector represents relative weights for the probability that numbers from 1 to 6 will be selected. At the beginning, they all have equal chances of choice. I go through each line of the while loop, explaining what it does:

  • The first line inside the while loop creates a cumulative probability from the probabilities vector. The CUMSUM function is used to return the cumulative sum over the length of the vector, and this is divided by the total amount of the vector (found using the SUM function). At the first passage of the cycle, cumprob will have the following values:

     0.1667 0.3333 0.5000 0.6667 0.8333 1.0000 

    Please note that they create “bunkers” into which a random number from 0 to 1 can fall. The probability that the number falls into a given bit is equal to the width of this bin, so there is a 1 in 6 (0.1667) chance that it’s random the dialed number will drop in the first hopper (from 0 to 0.1667), or in the second hopper (from 0.1667 to 0.3333), etc.

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

  • The third line inside the while loop adds "nonrandomness", shifting all relative weights up, moving the probabilities a little closer to the equal for all numbers. Consider an example where probabilities has the following form:

     [xxx 1 xx] 

    where x is a value greater than 1. At this moment, the probability of choosing a value of 4 is 1/(5*x+1) . Adding 1 to all elements, this probability becomes 2/(5*x+7) . For x = 3 probability of occurrence of 4 increases from 0.0625 to 0.0909, and the probability of any other number decreases from 0.1875 to 0.1818. Thus, this “nonrandomness” acts to normalize probabilities.

  • The fourth line inside the while loop essentially does the opposite of the previous line, significantly reducing the relative weight of any number that just happened, making it less likely in subsequent cycles. This reduced probability of occurrence will be short due to the effect of the previous line, constantly trying to return the probability of occurrence back to equal for all numbers.

    Note that the sum subtracted from one probabilities element is equal to the total sum added to all elements of the previous row, which leads to a net zero change for the total sum of the probabilities vector. This keeps probabilities limited so that they not only continue to grow and grow.

  • The if statement at the end of the while loop just needs to make sure all numbers in probabilities positive. If the minimum value of the vector (found using the MIN function) is less than zero, then this value is subtracted from each element of the vector. This ensures that the cumprob vector always has values ​​from 0 to 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 several times, you will see how the code does what it does. Here is one such set of 6 rolls that draws each of the numbers from 1 to 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 

Note that the final values ​​in probabilities are equal, which means that at this point all numbers 1 through 6 have an equal probability of choosing again.

+12
source

All Articles