Understanding Math.random() 🚁
Hi, *|USERNAME|*!
As we all spend time with our families and friends celebrating April Fools, I figured I'd reflect back on one of my favorite functions, the venerable Math.random(). While it is the subject of my latest tutorial, it has been a regular feature on the web site since the early Flash days. The Math.random() function's ability to generate a random number from 0 and (almost) 1 lends itself to many situations.
One common situation is animations, such as Animating Many Things on the Canvas:
By relying on random numbers, you can have your animations behave slightly differently each time they run or loop. You can see this effect maximized not just in the example we have above, but also in the Creating Falling Snow and the Irritated Bubbles deconstructions.
Looking beyond animations, have you ever wanted to shuffle the contents of an array? Generate a random color? Sort data in a more interesting way? All of these things wouldn't work as well as they do without relying on the random number generating capabilities of Math.random().
TILL NEXT TIME!
There are a lot of built-in functions in the JavaScript language. None are as exciting as our friendly random number generating one that is the star of this newsletter. Or are there others that you think deserve to be called out? Drop by the forums and chime in with your ideas or simply ping me via Twitter.
Cheers,
Kirupa 😀