Original illustration by Caro Bernardini .
We recently came across a situation where a media trafficking company (Google’s DoubleClick) gave us an ad specification saying you are not allowed to use “Random” within a banner. You can see their specifications here hidden under “Coded Behavior Constraints”:
“Random numbers: Ads may not include code that generates or uses random numbers.”
http://support.google.com/adwordspolicy/bin/answer.py?hl=en&answer=176108
As many developers might agree, that is quite a handicap as “random” not only helps aid visual animations, but is also necessary for adding a level of realism to the animations. To make things even worse, the QA tool that Google/DoubleClick uses (at the time of writing) does not take an entirely informed approach at scanning the code. It simply searches your code for the word “Random”. So even variable names like “randomAmount” had to be changed to “ranAmount” in order to pass their testing tool.
At first this seemed a daunting task, potentially impossible to accomplish without sacrificing the integrity of the animations. Especially since it was the Oxfam: You Are What You Give campaign which has a lot of character animations, and details like flies randomly moving around in the background.
There are a few other posts ranting about this specific problem, along with even less solutions which tended to be half-baked. This left us with some theories on how to solve it, but nothing solid, so we set off to create a solution.
The general idea was to create a function we could call (much like Math.random), which would return a value of seemingly random nature. The other tricky part is that, much like Math.random, you often need a random number within a certain range, including a decimal. For example Math.random () *20 will return a random number less than 20 along with decimals. Perhaps something like: 6.377002242952585
So our approach will:
More advanced users may want to throw this in a separate Class file, but for simplicity sake, you can simply copy this code and paste it into your actions.
// this is your index to keep place of where in the array you currently are private var ranPos:Number = 0; // this is your array that you can fill up with as many random number as you want. private var ranArray:Array = new Array ( 0.8207125988, 0.8858442162, 0.7519853998, 0.4565763700, 0.1330507124, 0.4981171825, 0.7353662381, 0.4396005486, 0.3280506672, 0.3485110882, 0.4537169698, 0.7402439945, 0.1728033694, 0.4304377208, 0.1022901596, 0.5108054741, 0.0951278760, 0.2321688487 ); // and this is the main function you call to grab a random number. For example getRan (20); private function getRan (ranAmount:Number=1):Number { // use the modulo % operator to reduce the number to within the range of whatever the user sent in. var num:Number = ranArray[ranPos]*1000 % ranAmount; // increase the position in the array that we are at. ranPos ++; // if we have gone ot the end of the array, start over if (ranPos >= ranArray.length-1) ranPos = 0; return num; }posted by Wonder Giant