Gotta love that title! Explanation at the end of this post.
This project develops a set of custom blocks that work together to produce a pair of "independent, standard, normally distributed (zero expectation, unit variance) random numbers."
You might find a use for numbers like that in a course on statistics, or in coding a simulation of certain kinds of natural phenomena. I don't go there in this post. Just sharing a tool, that's all.
A pair of custom reporter-type blocks defined in the Operators group do the work:
- random decimal fraction (see that Topic on this Forum)
- Box-Muller transform
The blocks implement a mathematical procedure called the Box-Muller transform (the BMT). I first read about it in Donald Knuth's wonderful book, Seminumerical Algorithms. This project actually takes its authority from Wikipedia's page on the topic. Search for Box-Muller transform.
The BMT block produces a pair of normally-distributed random numbers. It returns only one of the pair at a time. However, it retains the other value of the pair left over from one call and reports that value the next time.
Huh? How does it do that? The BMT is a lamda function, which means it returns not a number but a second function that, in turn, reports the numeric results.
Smart readers, please bear with me. I know that you know more about lambda functions that I do. I'm writing this to offer a beginner's delight for the benefit of readers who might know even less than me. In that spirit, please don't chide me for repeating what is obvious to you, but rather aid me by addresssing any errors I make here. Thanks!
Being a lambda function, the BMT allows for both results of the transform to be reported out, one at a time. The BMT block does this by remembering one of the pair during the time between calls to the secondary function. This design allows it to perform its calculations only once for each two values that it produces, which may reduce its burden on the computer's CPU.
EDIT: many thanks to @dardoro for discovering and correcting an error in the original code. The test and correction appear in the comments, below.
When the script is started by clicking the green flag, the secondary function that is returned by the BMT block is placed in a user-defined container named "standard normal deviate".
The value returned by that second function is then accessed by enclosing the container in a "call" block, as shown:
To see the code for this function, search the Berkeley Snap! web site for Box-Muller_transform, or load it directly with the following url:
Explanation of the title. If you took a Statistics class you might remember something called a Standard Deviation, a gauge of how far individual data points might be found away from the mean, or average, of all the data points. Standard deviate could be understood to measure how far one data point is away from the mean, using the Standard Deviation as the unit of measure. A standard normal deviate is that same measure, but for the special case where the mean is zero (0) and the Standard Deviation is one (1).
But still. Doesn't "standard normal deviates", sound like it describes some characters in a Far Side cartoon? I imagined using that name for this topic would attract more interest compared to naming it Box-Muller Transform, which is what it's actually about.