Shuffle Objects or Random Order In Flash

– taterboy | July 2nd, 2008

Filed under: ActionScript 2, ActionScript 3, Flash, Tips

Random is a funny thing. You are working on a site or a game and think, “let’s make this part random to add some interest. You add your new code and realize that now things are repeating where they were not before Or the game experience is now really inconsistent.

That is because random is well, random. It is not 1, 2, 3, 4 any more, but it is also not a consistent distribution or values ether. The first sample is a concept built to prove a point I felt very strongly about, that I wagered lunch on it. I lost, but it is still an interesting exercise.

Each graph produces random numbers from values 10, 100, and 1000 then counts how many times the random number is within 10%, 20%, 30% etc., of those values. The number in the Calculate button tells how many times the random numbers are produced for each value.

If you press the Calculate 10 button, the first column will show how many times the random numbers are produced from the value of 10, The second column uses the value of 100 and the third column shows the percentage of random numbers from the value of 1000.

The purpose of the test was to see if making random numbers from larger values resulted in a more even distribution of random numbers. Increasing the amount of each loop does help, so try out the Calculate 100, 500, 1000 and 2000 buttons as well. The Calculate 2000 button loops 6000 times and may cause Flash to hang if you are not running a good CPU, so press that one at your own risk.

The Second example shows how to use random to shuffle items so that they do not repeat. I wish iTunes would use this type of system for their shuffle control****.

Click the “Shuffle” button to rearrange the numbers and click “123 Order” to place them back in order.

The process is simple, build an array that will hold you list of Numbers, Names, or Object References. Then add a key to each section of the array, this will hold your random numbers. When the array is sorted to the key, all the items will be shuffled with no repeats.

?View Code ACTIONSCRIPT
function setOutput(val:int):void{
	shuffleArr = new Array();
	for(var i:uint = 1; i<= boxCnt; i++){
		var randKey:int = Math.random()*boxCnt;
		//trace(randKey);
                // Where id is is, place your data/object you want sorted.
		if(val == 0){
			shuffleArr[i-1] = {id:i,key:i};
		}
		else{
			shuffleArr[i-1] = {id:i,key:randKey};
		}
		shuffleArr.sortOn("key",Array.NUMERIC);
		var tCnt:int = 1;
		for(var t:String in shuffleArr){
			this[boxName + tCnt].op.text = shuffleArr[tCnt-1].id;
			tCnt ++;
		}
	}
	opt.text = "Status : Complete";
}

Sample 1 is AS2 and sample 2 is AS3, source for both below.
Here is the Source

****iTunes Shuffle Feature / Bonus Tip:
I work around for iTunes shuffle feature that randomly picks songs instead of using a true shuffle mechanism is to build two dynamic Playlists. The first one is a list of songs in the desired genres (Genre , is, someGenre). The second playlist if the one you will use to listen to which will include the songs from the first playlist (Playlist, is, firstPlayList). Then set the limit to [1000] [items] selected by [least recently played] .

Now your songs will cycle out of this playlist as they are played, you can also use shuffle control on this list.

Technorati Tags: , , , , , , , , ,

| No Comments » | facebook:

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

*