Cyberspace Nova

Random array in AS2 & AS3 example using sort

May 26, 2007 · 17 Comments

I suppose many of you know how to use sort to get random array…
For those who are not familiar with those nice feature of sort method I will explain the idea. Sort method can randomize array if you put function that return -1, 0 or 1 randomly, which basiclly tells sort randomly are elements less then, equal or greater then eash other.

AS2 Random Array based on Sort source:

var a:Array = new Array(“a”, “b”, “c”, “d”, “e”);
function shuffle(a,b):Number {
var num : Number = Math.round(Math.random()*2)-1;
return num;
}
var b:Array = a.sort(shuffle);
trace(b);

AS2 Random Array based on Sort source:

var a:Array = new Array(“a”, “b”, “c”, “d”, “e”);
function shuffle(a,b):int {
var num : int = Math.round(Math.random()*2)-1;
return num;
}
var b:Array = a.sort(shuffle);
trace(b);

EDIT: you can take a look at algo comparison, with source for much faster approach in new post

Categories: Actionscript 2.0 · Actionscript 3.0 _ · Applications · Flash · Flash _ · Flex · Web developer

17 responses so far ↓

  • rick // June 7, 2007 at 8:01 pm | Reply

    i tried your random adn it works great but i am trying to apply this to a multidimensional array and it doesn’t work. any suggestions?

  • mrsteel // June 7, 2007 at 10:30 pm | Reply

    how exactly you would like to sort a multidimensional array?

    give me an example and I will try to provide an example

    also I will soon post much more faster array sort method

  • rick // June 8, 2007 at 12:48 am | Reply

    well i ama creating a map out of a 2D array. Rows and columns like a checker board. the catch is it is simulating a 3D griad with walls and floor tiles. I gave each a numerical value of 100= floor and 200= wall. This is for a collision detection. If 200> cant go past. I want to keep the outer walls at 200 for a borader so all of row1 and lastrow will be 200 and the begining index and last index of every array in between will = 200. All the remaining indexes i want to randomize with the value of 100 or 200. Essentially creating a random maze.

  • mrsteel // June 8, 2007 at 1:00 am | Reply

    well, seems like this function won’t do it for you
    this is for randomizing an array (indexes of array) so
    a,b,c,d becomes b,c,a,d or d,c,a,b

    for your idea if I understood it good following lines could help

    for (var i:Number;i

  • mrsteel // June 8, 2007 at 1:04 am | Reply

    stupid comments
    can’t show AS
    link to text file
    http://www.hagane-studio.com/as/maze/maze.txt

  • rick // June 8, 2007 at 8:06 am | Reply

    I tried using your nested loop and tweeked it a bit. Now i can’t get the if statement to work. i keep getting a value of 200 in places i don’t want. Any ideas why?
    thanks

    // 2D array
    var gridsize:Number = 10;
    // creating the map
    var map_ray:Array = new Array(gridsize);
    var i:Number;
    var j:Number;

    //incrementing the col to = gridsize
    for (i=0; i

  • mrsteel // June 8, 2007 at 8:59 am | Reply

    heh,
    it’s not good writing a code from head
    I made few mistakes

    here are the examples
    http://www.hagane-studio.com/as/maze/maze.swf
    http://www.hagane-studio.com/as/maze/maze.fla

    I hope this solves it out

  • Alec McEachran // June 15, 2007 at 1:01 pm | Reply

    Sorry, but your array randomisation routine has problems. Using sort will not randomise the array in a complete manner, much like giving a pack of cards one shuffle won’t sufficiently randomise them to prevent long runs of cards.

    The following link explains:

    http://www.ptolemy.co.uk/archives/as3/7

  • Randomize Array - Shuffle an array in Flash « Cyberspace Nova // June 15, 2007 at 1:37 pm | Reply

    [...] developer , Flash _ , Actionscript 2.0 , Actionscript 2 , Flex , Flash After I’ve post solution for shuffling an array with Array.sort method which purspose was to just show what a sort function can be used for I am now posting [...]

  • mrsteel // June 15, 2007 at 1:42 pm | Reply

    don’t be sorry
    looking at your article and as I knowing math good I can agree it’s not ultimate array shuffle, but it’s good for beginners
    stating it won’t shuffle pack of cards good is not true
    pack of cards are shuffle good and good enough, taking it in long runs can lead to some cards show more times on one then another, that is true

    your splice algorithm lacks on speed, I made another article which I found time to do know after you made your point ;

    http://mrsteel.wordpress.com/2007/06/15/randomize-array-shuffle-an-array-in-flash/

  • Array Aleatorios en AS2 » unijimpe // January 6, 2008 at 4:53 am | Reply

    [...] ejemplo original lo encontré en Cyberspace Nova y es Random array in AS2 & AS3 example using sort, solamente he cambiado algunos nombres de variables para hacerlo mas sencillo de [...]

  • Array Aleatorios en AS2 « // January 9, 2008 at 10:03 am | Reply

    [...] ejemplo original lo encontré en Cyberspace Nova y es Random array in AS2 & AS3 example using sort, solamente he cambiado algunos nombres de variables para hacerlo mas sencillo de [...]

  • sylvia // March 6, 2008 at 6:30 pm | Reply

    nice work man 10x

  • Codesignist // July 19, 2008 at 4:17 pm | Reply

    Nice trick, thanx.

  • Agustin // November 4, 2008 at 10:54 am | Reply

    wow!!! just now.
    With your code I gave a second look to the Array.sort() method, and finally I can make sense to issues I had given up on.
    One never stops learning, even about things you thought you knew well.
    It’s a very efficient way to shuffle an array.
    Thanks a lot, I was looking for a way less elegant solution, this is great.

  • Tom Anderson // March 27, 2009 at 7:35 pm | Reply

    Using “while” instead of “for”, as in the following function, reduces the processing time from 4 ms to 1 ms! So the stats are (Methods 1-4 are original, Method 5 is the while loop):

    Method 1, time: 718
    Method 2, time: 1957
    Method 3, time: 3
    Method 4, time: 4
    Method 5 (while), time: 1

    /////////////////////////

    function mixArray(array:Array):Array {
    var _length:Number = array.length, mixed:Array = array.slice(), rn:Number, it:Number, el:Object;
    while (it < _length) {
    el = mixed[it];
    mixed[it] = mixed[rn = it + random(_length - it)];
    mixed[rn] = el;
    it++;
    }
    return mixed;
    }

  • Paul // June 9, 2009 at 5:03 am | Reply

    Amazing sir. This is by far one of the best solutions I’ve found yet.

    Thanks alot, really appreciate you providing this for everyone around you.

Leave a Comment