As3 random array - randomize array - actionscript 3

How do you fetch an array using actionscript 3?

+7
source share
8 answers

There is a short version using the 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 - line by line explanation:

For the Array class sort() method, you can pass not only sorting parameters, such as Array.CASEINSENSITIVE, Array.DESCENDING , etc., but also your own reference to the comparison function (callback), which takes two parameters (two elements from an array for comparison), From the AS3 documentation:

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

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

Note. The parameters of the comparison function can be printed (if your array is printed) and have whatever name you want, for example:

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

This method is very useful when you need to sort the elements of an array according to their special properties. In case of accident, compareFunction randomly returns -1, 0 or 1 and forces the elements of the array to switch to their places (indices). I found that the best randomization (in my subjective and mathematically untested opinion) is that the method returns only -1 and 1 . Also keep in mind that the sort function with a custom comparison function does not compare items sequentially , so in some special cases, the randomization results may differ from the expected ones.

+17
source

There's a better way that will also allow you to randomize the array in place if you need it, and it will not force you to create more than one copy of the 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; } } } 
+2
source

I found this very helpful. Hope this helps 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 :) 
+1
source

I had an alternative requirement when I wanted to randomly insert multiple source arrays into the 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); 
+1
source

here is easier. It also works with 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; } 
+1
source

If you need your array to be shuffled (your elements cannot be repeated). You can 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; } 

Using:

 var testArray:Array = ["Water", "Fire", "Air", "Earth"]; trace(shuffleArray(testArray).concat()); 
0
source

this is how i randomize my 36-card array 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............. } 
0
source

select 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; } 

using:

 trace(keyGenerator(16)); 
0
source

All Articles