
Welcome Guest
|
#1
|
|||
|
|||
|
SOLVED -- Help with a Tough Array Randomization/Shuffle Problem
I am currently 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: Actionscript:
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);
}In case you are interested, I've programmed it this way because the contents (numbers) of the base Array are changed from movie to movie. 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 of the available 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 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. Actionscript:
// Shuffles the array's elements in place.
function shuffle (theArray) {
for (var currentElement = 0; currentElement < theArray.length; currentElement++) {
// Remember the value of the current element.
var thisElementValue = theArray[currentElement];
// Pick a random index from the array.
var randIdx = myRandom(0, theArray.length - 1);
// Swap the element at randIdx with the current element.
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 oh so greatly appreciated. This one randomization issue has had me on the ropes for a few days now. TIA, TheMediaBoy Last edited by TheMediaBoy : 07-20-2006 at 01:07 PM |
«
Previous Thread
|
Next Thread
»
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| array problem | aquillian | Flash 8 |
3 | 05-03-2007 05:11 PM |
| Xml Parse to Array Problem Please Help | jdsflash | Flash MX ActionScript |
0 | 03-08-2006 08:20 PM |
| Array Problem | TerribleWay2Die | Newbies |
3 | 03-17-2005 08:20 PM |
| Problem with array | FLASHIMODO | Flash MX ActionScript |
2 | 10-07-2004 08:27 AM |
| sortOn() on an array of MovieClips: problem | aLearnerRather | Flash MX ActionScript |
3 | 01-06-2004 09:28 PM |

Programming Languages



