random Array generator class – AS3

16 01 2008

I went to an on site job interview and was given a list of video paths. The interviewer then asked me to generate a randomized play list with the given paths without duplicating any of them. It was a piece of cake for me to get it right, but I thought it is a useful trick and I’d make it a utility AS3 class and post it here.

Use this class to generate and return a new array with x number of elements in a random order, or return a new array which has the same set of elements as an existing array at a randomized order. See below:

package com.interactiveSection.utils{
	public class RandomArray{
		public function RandomArray():void{};
		public static function generateRdmArray(numElements:int, origArray:Array=null):Array{
			if (origArray==null || numElements>origArray.length) { 
				origArray = (origArray==null)? new Array():origArray;
				for (var i:int = origArray.length; numElements>i; i++){
			var tempArray:Array = new Array();
			tempArray = origArray.slice();
			var resultArray:Array = new Array();
			while (tempArray.length>0 && numElements>resultArray.length){
				var rdm:int = Math.floor(Math.random()*tempArray.length);
			trace("returning generated array: "+resultArray);
			return resultArray;

[+] input:
possible output:
returning generated array: 1,5,4,2,3,0

[+] input:
RandomArray.generateRdmArray(6, [“LA”, new MovieClip(), “bb”, 90000, new Object(), [0,1,2] ]);
possible output:
returning generated array: LA, 0,1,2, bb, [object Object], 90000, [object MovieClip]
(Note: here 0,1,2 together make one element (an array with 3 elements) in the result array, which has six elements in total)

[+] input:
RandomArray.generateRdmArray(3, [“a”,”b”,”c”,”d”,”e”]);
possible output:
returning generated array: a,c,d
(Note: the original array has more elements than the result array, so just pick 3 random elements from the original array to make the result array )




4 responses

24 12 2008
Rob Davis

Thought you might find this useful. I divided your class up into two methods to simplify each one:

package com.solarismedia.utils {

public class Array2 {

public function Array2()

* Takes an existing array and returns a new array with the elements in the original
* array placed in random order.
public static function randomise(array:Array=null):Array{
var tempArray:Array = [];
tempArray = array.slice();
var resultArray:Array = new Array();
while (tempArray.length > 0 && array.length > resultArray.length){
var n:int = Math.floor(Math.random() * tempArray.length);
//trace(“returning generated array: “+resultArray);
return resultArray;

* Generates a new array of digits from 0 to size, but in random order. Can be used
* to create a sequence of random events, where each event only happens once per cycle.
public static function generateRandomArray(size:int):Array{
var tempArray:Array = new Array();
for (var i:int = 0; i < size; i++) {
return Array2.randomise(tempArray);

5 01 2009

@ Rob: Thanks. This is helpful. 🙂

17 07 2009

thank you!! 😀

24 01 2011

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: