Processing
 

as3 random array - randomize array - actionscript 3

16/08/2012 04:18#1

Joined at: 9 months ago

Post: 62

Thank: 0

Thanked: 0

How do you randomize an array using actionscript 3?

16/08/2012 07:45Top#2

Joined at: 9 months ago

Post: 21

Thank: 0

Thanked: 0

There is a short version using Array.sort() function:

var arr : Array = [0,1,2,3,4,5,6,7,8,9];  function randomize ( a : *, b : * ) : int {     return ( Math.random() > .5 ) ? 1 : -1; }  trace( arr.sort( randomize ) ); 

If you don't get "enough" randomness you can sort twice :)

EDIT - explanation line by line:

For Array class method sort() you can pass not only sort options like Array.CASEINSENSITIVE, Array.DESCENDING and so on but also your own custom compare function reference (a callback) that accepts two parameters (two elements from array to compare). From AS3 documentation:

A comparison function should take two arguments to compare. Given the elements A and B, the result of compareFunction can have a negative, 0, or positive value:

  • A negative return value specifies that A appears before B in the sorted sequence.
  • A return value of 0 specifies that A and B have the same sort order.
  • A positive return value specifies that A appears after B in the sorted sequence.

Note: compare function parameters might be typed (if your array is typed) and have any name you want eg.:

function compareElements ( elementA : SomeClass, elementB : SomeClass ) : int; 

This method is very useful when you need to sort array elements by their special properties. In randomization case compareFunction randomly returns -1, 0 or 1 and makes array elements to switch their places (indices). I have found that better randomization (in my subjective and mathematically untested opinion) is when method returns only -1 and 1. Also have in mind that sorting function with custom compare function doesn't compare elements sequentially so in some special cases randomization results may differ from what you might expect.

16/08/2012 07:14Top#3

Joined at: 10 months ago

Post: 174

Thank: 0

Thanked: 0

There's a better way that will also allow you to randomize the array in place, if you need that, and it will not make you create more then a single copy of your original array.

package {     import flash.display.Sprite;      public class RandomizeArrayExample extends Sprite     {         public function RandomizeArrayExample()         {             super();             testDistribution();         }          private function testDistribution():void         {             var hash:Object = { };             var tester:Array = [1, 2, 3, 4];             var key:String;              for (var i:int; i < 1e5; i++)             {                 randomize(tester);                 key = tester.join("");                 if (key in hash) hash[key]++;                 else hash[key] = 1;             }             for (var p:String in hash) trace(p, "=>", hash[p]);         }          private function randomize(array:Array):Array         {             var temp:Object;             var tempOffset:int;             for (var i:int = array.length - 1; i >= 0; i--)             {                 tempOffset = Math.random() * i;                 temp = array[i];                 array[i] = array[tempOffset];                 array[tempOffset] = temp;             }             return array;         }     } } 
16/08/2012 15:41Top#4

Emile

Member

Joined at: 10 months ago

Post: 27

Thank: 0

Thanked: 0

I had an alternative requirement where i wanted to randomly insert lots of source arrays into a target array randomly. Like Rytis i'm a big fan of the forEach, map and sort functions on Arrays.

var randomInsert:Function = function callback(item:*, index:int, array:Vector.<MyItem>):void {     var j:Number = Math.floor(Math.random() * targetArray.length);     targetArray.splice(j,0,item);                    }  targetArray = new Vector.<MyItem>(); sourceArray1.forEach(randomInsert, this); sourceArray2.forEach(randomInsert, this); 
13/09/2013 11:07Top#5

Tien Lu

Member

Joined at: 7 months ago

Post: 2

Thank: 0

Thanked: 0

here's an easier function. Works also on multidimensional arrays

function randomizeArray(array:Array):Array {     var newArray:Array = new Array();     while (array.length > 0)     {         var mn=Math.floor(Math.random()*array.length)         newArray[newArray.length]=array[mn]         array.splice(mn,1)     }     return newArray; } 
16/08/2012 04:19Top#6

Joined at: 9 months ago

Post: 62

Thank: 0

Thanked: 0

I found this very helpful. I hope it can help you too.

// Array to Randomize var firstArray:Array = ["One","Two","Three","Four","Five","six","seven","eight","nine","ten"]; trace(firstArray); // Prints in order  var newArray:Array = new Array(); function randomizeArray(array:Array):Array {     var newArray:Array = new Array();      while (array.length > 0)     {         newArray.push(array.splice(Math.floor(Math.random()*array.length), 1));     }      return newArray; }  var randomArray:Array = randomizeArray(firstArray); trace(randomArray); // Prints out randomized :) 
06/09/2014 17:16Top#7

Glogo

Member

Joined at: 6 months ago

Post: 1

Thank: 0

Thanked: 0

If you need your array to be shuffled (your elements can not repeat). You could use this function:

/**  * Shuffles array into new array with no repeating elements. Simple swap algorithm is used.  */ public function shuffleArray(original:Array):Array {     // How many swaps we will do     // Increase this number for better results (more shuffled array, but slower performance)     const runs:int = original.length * 3;     var shuffled:Array = new Array(original.length);      var i:int;     var a:int;     var b:int;     var temp:Object;      // Copy original array to shuffled     for(i=0; i<shuffled.length; i++){         shuffled[i] = original[i];     }      // Run random swap cycle 'runs' times     for(i=0; i<runs; i++){         // There is a chance that array element will swap with itself,         // and there is always small probability it will make your shuffle         // results not that good, hence try to experiment with         // different runs count as stated above         a = Math.floor(Math.random() * original.length);         b = Math.floor(Math.random() * original.length);          // Swap messages         temp = shuffled[a];         shuffled[a] = shuffled[b];         shuffled[b] = temp;     }      return shuffled; } 

Usage:

var testArray:Array = ["Water", "Fire", "Air", "Earth"]; trace(shuffleArray(testArray).concat()); 
15/12/2014 14:34Top#8

Surtarso

Member

Joined at: 6 months ago

Post: 1

Thank: 0

Thanked: 0

this is how I randomize my array of 36 cards for a memory game

const QUANT_CARTAS: int = 36;  //get the 36 numbers into the array for (var i: int = 0; i < QUANT_CARTAS; i++) {     cartas.push(i); }  //shuffles them =) for (var moeda: int = QUANT_CARTAS - 1; moeda > 0; moeda--) {     var pos: int = Math.floor(Math.random() * moeda);     var carta: int = cartas[moeda];     cartas[moeda] = cartas[pos];     cartas[pos] = carta; } // and add them using the random order...      for (i = 0; i < QUANT_CARTAS; i++) {     var novaCarta: Carta = new Carta();     novaCarta.tipoCarta = cartas[i];     etcetcetc............. } 
10/02/2016 11:43Top#9

Joined at: 6 months ago

Post: 1

Thank: 0

Thanked: 0

choose random string from array

function keyGenerator(len:Number):String {     function randomRange(minNum:Number, maxNum:Number):Number     {         return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);     }     var hexArray = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];     var key = "";     for (var i=0; i<len; i++)     {         key +=  hexArray[randomRange(0,hexArray.length-1)];     }     return key; } 

usage:

trace(keyGenerator(16)); 
Similar articles

Open a folder in finder/explorer from a webpage?

5 years ago - Reply: 2 - Views: 80

How do I escape a reserved word in Actionscript?

5 years ago - Reply: 2 - Views: 198

Save PPS/PPT files as SWF using C#

5 years ago - Reply: 2 - Views: 171

Ways of making a graphic/visual novel in flash

6 years ago - Reply: 3 - Views: 114

Use PhoneGap + HTML5 and an Embedded Flash Movie

5 years ago - Reply: 2 - Views: 96

View word docs and PDF in browser?

5 years ago - Reply: 2 - Views: 51

ActionScript 3 Read JPEG quality

7 years ago - Reply: 2 - Views: 59

Export/Test Movie Issues

5 years ago - Reply: 3 - Views: 38

Flash always on top in Android browser workaround?

5 years ago - Reply: 2 - Views: 149

Sending Json Form from Flash AS3

5 years ago - Reply: 2 - Views: 29

parsing a swf/fla (using php?)

6 years ago - Reply: 2 - Views: 44

Getting high FPS on Flash/AIR games for mobile?

4 years ago - Reply: 2 - Views: 58

vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re