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++){
origArray.push(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);
resultArray.push(tempArray[rdm]);
tempArray.splice(rdm,1);
}
trace("returning generated array: "+resultArray);
return resultArray;
}
}
}
Examples:
[+] input:
RandomArray.generateRdmArray(6);
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 )

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()
{
super();
}
/*
* 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);
resultArray.push(tempArray[n]);
tempArray.splice(n,1);
}
//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++) {
tempArray.push(i);
}
return Array2.randomise(tempArray);
}
}
}
@ Rob: Thanks. This is helpful.
thank you!!