Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 07-19-2006, 06:13 PM   #1
TheMediaBoy
TheMediaBoy
 
Join Date: Jul 2006
Posts: 1
Default Help with a Unique Array Sorting/Randomization Problem

I am working on a project where I have a base Array that consists of 5 numerical elements. Using this base array, I use a for loop to create a new Array consisting of 250 elements (50 interations of the base array).

Here is the code, which works flawlessly:

Code:
var myArray:Array = new Array ();
 
// Loop through base elements to load the new array.
for (var i:Number = 0; i < 50; i++) { // 50 = multiples of base array to equal 250 elements
	// push the designated elements into myArray
	myArray.push (48, 49, 50, 56, 57);
}

My issue now is that I need to shuffle or randomize this new Array (250 elements) so that the same number (base Array element) does not appear consecutively. I've located several randomization and shuffling code examples, but I can't seem to customize any of them to fit my particular need.

Here is what I am currently using, but I suspect that because of the limited number of base elements (5), I still get several instances of the same element appearing consecutively..., sometimes even 3 or 4 times consecutively.


Code:
// Shuffles the array's elements in place.
function shuffle (theArray) {
	for (var currentElement = 0, previousElement = currentElement; currentElement < theArray.length; currentElement++){
		// Remember the value of the CURRENT array element
		var thisElementValue = theArray[currentElement];
		// Remember the value of the LAST array element
		var lastElementValue = theArray[previousElement];
		// Pick a random index from the array
		var randIdx = myRandom(0, theArray.length - 1);
		// Ensure the selected array value does NOT match the previous array value
		if (thisElementValue != lastElementValue) {
			theArray[currentElement] = theArray[randIdx];
			theArray[randIdx] = thisElementValue;
		}
	}
	myNewArray = theArray;
}

// Returns an integer in the minVal to maxVal range.  Used in the array shuffle.
function myRandom (minVal, maxVal) {
	do {
		// Keep picking a number until it is not 1
		var r = Math.random();
	} while (r == 1);
	return minVal + Math.floor(r * (maxVal + 1 - minVal));
}
// Initiates the Array shuffle function from above
shuffle(myArray);

Any suggestions, tips, or just pointing me in the right direction would be greatly appreciated. This one issue has been kicking my butt for a day or so now, so it's time to pose the question to the real experts.


TheMediaBoy
TheMediaBoy is offline   Reply With Quote
Old 06-10-2007, 01:04 AM   #2
sarumitai
Senior Member
 
Join Date: Apr 2007
Location: Japan
Posts: 111
Default Found this somewhere

function shuffle(a,b){return random(2);}

a = ["pig",",mule","goat","hat","dog","cat"];

a.sort(shuffle);
trace(a);
sarumitai is offline   Reply With Quote
Old 06-11-2007, 05:58 PM   #3
kmcgurn
Registered User
 
Join Date: May 2006
Posts: 18
Default

i had to do this in a recent project. Here are the steps i used:

1. Create a temp array from the original one
myArrayTemp = myArray.slice();
2. Grab a random entry from the temp array
arrayTempNum = Math.floor((Math.random()*myArrayTemp.length));
3. Remove that entry from the temp array
myArrayTemp.splice(arrayTempNum, 1);
4. If the temp array now contains less than 2 entries, recreate the array as you did in step 1. If not, go to step 2 again.
if (myArrayTemp.length<2) {
myArrayTemp = myArray.slice();
}


the only way this can show the same element two times in a row is if the last element grabbed from the temp array is also the first one grabbed from the refreshed temp array. The odds of this are 1 in 250 squared but it can happen.
kmcgurn is offline   Reply With Quote
Old 08-09-2007, 08:04 PM   #4
pisosse
Senior Member
 
Join Date: Feb 2005
Location: copenhagen, Denmark
Posts: 104
Default

I found this over at flash kit

i = 1;
while (Number(i)<>11) {
rnd = random(10);
x = 1;
while (Number(x)<>Number((Number(length(Temp))+1))) {
if ((substring(Temp, x, 1)) eq rnd) {
rnd = "";
}
x = Number(x)+1;
}
if (rnd ne "") {
Temp = Temp add rnd;
i = Number(i)+1;
}
}
trace(Temp);

but it wont publish in player 8 only player 5.. any reason why?
pisosse is offline   Reply With Quote
Old 11-27-2008, 09:37 AM   #5
ThomasJamesT
Registered User
 
Join Date: Nov 2008
Posts: 2
Default

Quote:
Originally Posted by pisosse View Post
I found this over at flash kit

i = 1;
while (Number(i)<>11) {
rnd = random(10);
x = 1;
while (Number(x)<>Number((Number(length(Temp))+1))) {
if ((substring(Temp, x, 1)) eq rnd) {
rnd = "";
}
x = Number(x)+1;
}
if (rnd ne "") {
Temp = Temp add rnd;
i = Number(i)+1;
}
}
trace(Temp);

but it wont publish in player 8 only player 5.. any reason why?
Let arr1 be a the new array that will contain only unique values
Let arr2 be an array of elements that you randomize over. doesnt matter wether this one is already unique or not. what u want to escape i guess is that when for example
randomizing in a loop u put the same value twice in arr1. use method below to avoid that. in your other method: if returned boolean unique is true, then do what u intended, if its false, for example, do not increment loop and try again...


Note im using id and stringID here as they are object properties of my indexed array. You can of course compare on any other thing between the two arrays or just compare them directly

// arr1 new compare array
// arr2 original array, index: the index in that array we are comparing with all previous arr1 items
public function checkIfArrayUnique (arr1: Array, arr2: Array, index:Number): Boolean{
var unique : Boolean = true;
if (arr1.length >0){
for (var p: Number=0; p< arr1.length; p++) {
if (arr1[p].id == arr2[index].stringID) {
unique = false;
}
}
}
trace ("unique is " + unique);
return unique;
}

paz

thomas thorstensson

Last edited by ThomasJamesT; 11-27-2008 at 09:40 AM.
ThomasJamesT is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
menu = array; loop; problem?? visualstylee ActionScript 2.0 1 06-21-2005 08:28 PM
Array problem emedia ActionScript 2.0 4 06-20-2005 06:24 AM
Loading movies from an array problem. BCW ActionScript 1.0 (and below) 5 04-11-2005 02:28 AM
get unique array BenB ActionScript 1.0 (and below) 1 12-11-2003 11:27 AM
problem with going thru array (for mp3 player) jeeves ActionScript 1.0 (and below) 5 05-13-2003 02:24 AM


All times are GMT. The time now is 09:25 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.