Generating a string of random numbers in ActionScript 3

February 13, 2010

Actionscript 3.0 Tutorial

Now working on a new project and I needed a string (array) of random numbers are not repeated (shuffle)

It might be useful and to others.

var myNumbArray:Array = new Array();
var myRandArray:Array = new Array();
var myNumb:int = 20; //cat de lung vrei siru

function randRange(min:Number, max:Number):Number {
	var randomNum:Number = Math.floor(Math.random()*(max-min+1))+min;
	return randomNum;
}

for( var i:int = 0; i < myNumb; i++){
	myNumbArray.push(i);
}

while (myNumbArray.length>0) {
 	myRandArray.push( myNumbArray.splice(randRange(0, myNumbArray.length-1), 1));
}

trace(myRandArray)

Of course you can do and otherwise, probably over a year if I need her the same thing, not to remember how I solved the problem and have to solve otherwise.

, , , , , , ,

Subscribe

Subscribe to our e-mail newsletter to receive updates.

3 Responses to “Generating a string of random numbers in ActionScript 3”

  1. surfergirl_374 Says:

    Thanks!! You saved me!

    I converted it to AS 2 for those who havent reached to as3 yet:

    myNumbArray= new Array();
    myRandArray= new Array();
    myNumb = 21; //cat de lung vrei siru

    function randRange(min:Number, max:Number):Number {
    var randomNum:Number = Math.floor(Math.random()*(max-min+1))+min;
    return randomNum;
    }

    for( i=1; i 0) {
    myRandArray.push( myNumbArray.splice(randRange(0, myNumbArray.length-1), 1));
    }

    trace(myRandArray);

Trackbacks/Pingbacks

  1. Photoshop Video Tutorials. | myfapturboforex.com - February 13, 2010

    [...] Generating a string of random numbers in ActionScript 3 | Flash … [...]

  2. Generating a string of random numbers in ActionScript 3 | Flash … at Flash Designers - February 13, 2010

    [...] here:  Generating a string of random numbers in ActionScript 3 | Flash … [...]

Leave a Reply