Not yet rated

Problem

I am looking for the fastest way to generate unique random integer array.

Solution

Just push math random number into an array, and sort in NUMERIC and RETURNINDEXEDARRAY mode.

Detailed explanation

// generate unique random integer array from 0 to (n - 1)
function genRandomArray(n:int):Array {
var ary:Array = [];
while (n--) ary.push(Math.random());
return ary.sort(Array.NUMERIC | Array.RETURNINDEXEDARRAY);
}

var ra:Array = genRandomArray(10000);
trace(ra[0]); // get first unique random integer
trace(ra[1]); // get second unique random integer

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes