Use the Proper Function for Random Numbers… or else

by carl schooff on February 15, 2011 · View Comments

in AS3 Tips,What I Learned Today

This movie requires Flash Player 9

Back on topic. Got a real doozy for you today. This simple lesson can have some huge impact on the integrity of your Flash or javascript apps. I recently found out that I’ve been using a faulty random number function for ages. Take a look and see how one little function can make a huge difference.

Watch the video

It seems the randomNumber function I have been using to get a random number between a low and high value is very biased AGAINST the numbers at the far ends of the spectrum. Simply put, if I want a random number between 1 and 5 the chance that a 1 or 5 will show up is MUCH LESS than getting a 2,3, or 4.

The Faulty Function (NEVER USE THIS AGAIN)

function randomNumber(min:Number, max:Number):Number {
   	//bad
   	return Math.round(Math.random() * (max - min) + min);
}

The Accurate ActionScript 3 Random Number Function

function randomNumber(min:Number, max:Number):Number {
	//good
	return Math.floor(Math.random() * (1 + max - min) + min);
}

To use this function to generate a value between any where between and including 1 and 5

randomNumber(1,5);

Learn More

To read about the details of the function visit Kirupa: Random Numbers by Scott Ruttencutter

Special thanks to Matthew Lein for making me aware of this issue. Check out Matt’s JavaScript Experiments. He does really cool stuff with js.

Still unclear? scroll up and watch the video again:)

Download my test file used in the video

randomGoodness.fla

Thanks for watching. For a fun time, click each one of the pretty buttons below:

Post to Twitter Post to Facebook

  • Snefer

    hm i am a little bit disapointed ;f
    if u run off the ideas, i would love to see way of not clunky collision detection

  • Martin

    Great tutorials Carl, learnt more from your tuts than all the 15 books I have on AS3!! have a great way of mixing real life scenarios with the code, thanks again – MC

  • http://www.snorkl.tv/ carl schooff

    thanks martin, it is so wonderful to hear that. I’m glad I can fill in some
    of the gaps in the AS3 books.
    I’ve found that many AS3 books leave me in the dust after a few chapters.

    Books are great for getting really specific and detailed information about a
    particular topic, but I feel that few of them have examples that are fun and
    quick enough to really get you excited. I’m currently re-reading
    http://www.amazon.com/Object-Oriented-ActionScript-3-0-Todd-Yard/dp/1590598458and
    am enjoying it quite a bit. Some of these more complex concepts can
    take
    a while to sink in for sure.

    i’m hoping to tinker with a few tutorials incorporating some more oop
    concepts in the future.

    Carl

  • http://twitter.com/viaria Emrah Atılkan

    very nice to see that.

  • Haje

    It is quite natural that round does not give the same random numbers like floor. Let us look at an example with just one floating point. From 0 to 2 you could get 0.0, 0.1, 0.2 and so forth up to 1.9. With Math.round you get 0 for the numbers 0.0 to 0.4 (5 different numbers), 1 for the numbers 0.5 to 1.4 (10 different numbers) and 2 for 1.5 up to 1.9 (5 different numbers). In general, the smallest and the highest number is half as likely as all other values.

    Be aware that you do not get really random numbers – they are always calculated. Therefore, they are called pseudo random numbers.

    Btw: to avoid errors, your randomNumber function should test for the highest and the smallest number. If, for some reason, you provide as arguments the highest number first, the function does exclude both values from the random result. Inside randomNumber, use Math.max and Math.min to get the resprective numbers

  • http://profiles.google.com/marcelmarnix Marcel Marnix

    If I play a game with daiseys and dices, I would bet only on 2,3,4,5 ;)

  • http://www.snorkl.tv/ carl schooff

    smart man! :)

blog comments powered by Disqus

Previous post:

Next post: