Comparing number distributions
I use randomness quite often. To fluctuate the radius of ellipses, to select a bunch of colors or to change the position of an object. The go-to-guy for this in Processing is random(). If you need something a little smoother you go for noise(). These are probably the two most used functions to generate random numbers. Another way to get numbers on-the-fly is to use sin() in combination with frameCount. Note however that the numbers generated from this last method are NOT random.
To gain a little more insight into the respective number distributions, I decided to -visually- compare these different methods. This knowledge would be useful if for example you need a number distribution that favors highs. Or one that is evenly divided. Or something else. I wrote a sketch that counts a range of numbers generated by different methods and then visually represents these. Below you can find the results of my comparison. Note that the falloff value for noise() can be set via noiseDetail(). The default value is 0.50, which also gives the smoothest distribution.
Table 1 – Conclusions | |
Random | Evenly divided, each number from the range has an equal chance of being generated |
Sinus | Strongly favors the highest number from the range and also slightly favors highs |
Noise (0.10) | Strongly favors lows, several high numbers are never generated |
Noise (0.50) | Favors middle values, smoothly shaped distribution |
Noise (0.90) | Favors highs, +/- 30% of numbers generated are outside of (higher than) the range |
The embedded video unfortunately only plays at low resolution. Watch the HD-version here.
Table 2 – Code used for generating the numbers (int numbers = 10) | |
Random | numberGen = int(random(numbers)); |
Sinus | numberGen = int(numbers*abs(sin(frameCount))); |
Noise (0.10) | noiseDetail(4,0.10); numberGen = int((numbers+1)*noise(frameCount)); |
Noise (0.50) | noiseDetail(4,0.50); numberGen = int((numbers+1)*noise(frameCount)); |
Noise (0.90) | noiseDetail(4,0.90); numberGen = int((numbers+1)*noise(frameCount)); |