I have been in the midst of a heavy new screensaver project lately. I just discovered the most amazing little algorithm of my own invention. Many of my projects have involved tinkering with manifold parameters that I let vary randomly within certain ranges. By confining the bounds of my random generators I can get variability but also fine tune the parameters to get interesting results. It is a tedious process and lacking in real control however. I needed something better.
Along the way, it occured to me that it would be even more interesting if I could use a weighted distribution. I didn't know how to do this, because the whole idea of a pseudo random number generator is that it gives a flat distribution of equal likelihoods. I knew how to graph a bell curve etc. but was unaware of any way to actually generate one with random samples. The solution turned out to be super elegant and simple and I have to share it with you. It took only four lines of code excluding error checks and initialization. Heres the concept:
1. start with the range you want to consider and pick a target value in that range.
2. Generate a random number in that range. This number is of course one of many in a flat distribution
3. Get the difference between that number and the target
4. Let that value be the new range and generate a new random number within it.
That's all it takes!!!
I didn't know if it would work though so I had to test it. I wrote a short piece for my graphing program and generated 5000 test samples and plotted the distrubutions! It worked beautifully but was a bit peaky at the target. What was wrong? Maybe a small weighting factor was needed? Aha simply multiply the difference value above by some number 0.0 - 1.0 say and then generate.
It was astounding! at wt = 0.0 I got the pure straight out flat distribution
At wt = 1.0, I got the original pointy peak.
At wt = 0.5, I got a really nice rounded bell curve!!!
At wt = something > 1.0, I got a really sharp high-Q peak at the target value
At wt < 0.0, guess what I got? A very curious flat step function with the binary high side on the side less than target value. The more negative the wt, the greater the step difference!!!
The above was a totally unexpected consequence. It really gives me pause to think about the significance of the whole concept. It might have real value in a neural net model where the squashing function could be arbitrarily shifted by degree through those changes. Philosophically I'm also intrigued by the idea of a sort of recursive random that zeros in on some attractor value. Is this indeed the real source of the natural bell curve to begin with? Random orbits around strange attractors that recursively narrow their scope of randomness ( randomness as a function of parameters) till in the final analysis, they clump in value weighted toward the attractor. The negative wt step distribution might even represent the switch point where the orbits jump attractors.
The extreme simplicity of the code blew me away, I just had to talk about it. It may even be a moronic trick known to all statisticians and mathematicians but it was a real discovery for me!