Actionscript 2.0, Actionscript 3.0 _, Applications, Flash, Flash _, Flex, Web developer

Random array in AS2 & AS3 example using sort

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

About these ads
Standard

27 thoughts on “Random array in AS2 & AS3 example using sort

  1. rick says:

    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?

  2. 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

  3. rick says:

    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.

  4. 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

  5. rick says:

    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

  6. Pingback: Randomize Array - Shuffle an array in Flash « Cyberspace Nova

  7. 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/

  8. Pingback: Array Aleatorios en AS2 » unijimpe

  9. Pingback: Array Aleatorios en AS2 «

  10. Agustin says:

    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.

  11. 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;
    }

  12. 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.

  13. Pingback: 2010 in review « Cyberspace Nova

  14. As Alec McEachran seems to explain (although his link is broken), this function is slick and fast, but it’s not good if you want an even randomization. What I call an even randomization is for each item, to have a probability of 1/N to be found at any place in the randomized array.

    So if you’re going to call this function several times and want each result as different as possible from the previous, you should consider the splice method instead.

    More explanation can be found here :
    http://snipplr.com/view/49292/beware-with-array-shuffle-function/

  15. Hi my loved one! I wish to say that this post is awesome, nice written and come with approximately
    all significant infos. I’d like to peer extra posts like this .

  16. Good day! This is my first comment here so I just wanted to give a quick
    shout out and tell you I truly enjoy reading through your posts.
    Can you suggest any other blogs/websites/forums that go over the
    same topics? Thanks a ton!

  17. When I originally commented I clicked the “Notify me when new comments are added” checkbox and
    now each time a comment is added I get four e-mails with the same comment.

    Is there any way you can remove people from that service?
    Many thanks!

  18. sandy says:

    Hey can you help me to create 2 elements randomize array or pick two indexed array and put it randomly
    so it should be like this

    before using random function [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
    after using random function [ 5, 6, 1, 2, 9, 10, 3, 4, 7, 8 ] or [9,10,1,2,7,8,3,4,5,6]
    it always produce 1 before 2, 3 before 4, 5 before 6 and so on. Then put it randomly
    it will be impossible to produce 2,1 or 10,9 or 6,5 and so on,

    it will random 2 elements, not 1 by 1 but 2 by 2. Sorry for my bad English. I hope you understand what i mean.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s