uniquerandom

Please visit Straf Spul's main site. This is the nerdy corner.

Javascript
compositing
Actionscript
Picturepusher
Unique Random
swfLauncher
PHP
ls2xml
﻿

Enter some numbers and hit the shuffle-button. Then notice that each number appears only once.

### Series of unique random numbers

The Math.random method in flash has been the cornerstone to many flash - pieces around the world wide web. Letting a script pick one number is almost like as if the computer has the power to decide something for himself. These numbers can represent coördinates in a drawing, indexes of an array that can contain various kinds of objects, pictures, soundfiles, videos... By randomizing these Numbers the computer creates for us patterns, playlists, slideshows,...

Letting a program pick one single random number ain't that hard. In actionscript 2.0 we used to write those things like this:

randomNumber = random(10);

Then actionscript would return us an integer between 0 and 10.
However in actionscript 3.0 the return value for the Math.random method is a float between 0 and 1.
Something along the line of (mind the dot!):

0.5587153541855514

So if we want to get a random integer between 0 and 10 in actionscript 3.0, we can use this line:

var randomNumber:Number = Math.floor(Math.random()*10);

If we want an array that is a series of 10 random numbers between 0 and 9. We could loop it in a for-statement:

However if you trace the array randomNumber, it will output the following series of numbers:

5,4,4,1,2,5,3,7,0,2

You notice immediatly that the digits 4, 2 and 5 appear two times in the series and that the digits 6, 8 and 9 are excluded. Suppose those numbers were randomized id's of pictures in a slideshow. Some pictures would not appear in the slideshow, while others might appear twice.

So how do we build in a check to see if a value isn't already picked up by a previous number?

It's a bad idea, to compare each new number with all the previous created numbers.
If i were you i would NOT COMPILE the following code:

Chances are high that the program will crash or get stuck into an infinite loop, before it reached that last number.

The secret for creating a series of random numbers that are also unique, is not to include the check as you will see in the following...

### Re-usable public static function

Creating small classes with static functions enables us to re-use these methods as utils in all different kind of programs. Just drop the as-file into the same folder as your document class and import the randomUnique class at the beginning of your code with the following line of code:

import randomUnique;

And then call the function like this:

var myRandomNumbers:Array = randomUnique.between(0, 9);

Here's a simplified example:

So what is this code's ...

### Secret ingredient

We are going to work with two arrays here (baseNumber and randNumber). The first one will contain all numbers the script can choose from. In the first for-loop, this array is filled with those numbers. After that we use another for-loop that puts all numbers in a random order into the randNumber-array.

The second for-loop starts with the last number and counts down, filling up the randNumber-array backwards. Each time creating a new random number between the startNumber and a number that is smaller each time the for-loop iterates. Instead of directly putting this temporary random number in randNumber[i], the script assigns the value of baseNumber[tempRandom].

Because the value of baseNumber[i] is not within reach of the random picking the next time the for-loop iterates, we put the value of baseNumber[i] into baseNumber[tempRandom]. This way we create a memory for the script which says which numbers it can still choose from.

You see this is more elegant solution then letting the script pick a new random number if it was already picked, which can result in an infinite loop of number picking with nested loop statements.

If you want to download final class for re-use in your projects you can find it here. If you want to examine this page's simple interface example, you can download it here.

That's all, folks! I hope this code-example was usefull and saved you some headaches. It's actually really simple if you understand the trick to it.

Quinten Clause