Generating Skewed Random Numbers
A quick and dirty way to generate skewed random numbers from when I needed test data for a game.
Skewing random numbers with quadratics and exponentials
A quick way to skew random numbers towards 0, i.e. to make lower numbers more likely, is to raise the random numbers to a power - the higher the power, the higher the skew. To skew the other way, i.e. to make higher numbers more likely, just subract from 1
Or you could use an exponential for more drastic curves.
Obviously not the most efficient way of generating random numbers, but could be useful in certain occasions where performance is not an issue.
A basic class to generate skewed random numbers
This gist is a rough and ready class to test random numbers. It takes a map functions, generates 10 numbers, and pretty prints them to the console.
Here’s the output of a run. Not very conclusive, I have only printed 10 numbers because of space.
1 2 3 4 5 6 7 8
Visualizing distribution with Canvas
Just for a laugh (stop sniggering at the back), I thought I’d try some primitive HTML Canvas visualizations. The CodePen below runs the same code on 1000 sample points, and uses vertical lines to show their distribution.
That’s all folks
Nothing too exciting.