posted on Mar, 17 2012 @ 02:11 AM
Not PHP, but it's a script. Well, _javascript at least. Just a simple function of sorts. Maybe not pro either (I wouldn't be surprised if it's
considered clumsy coding), but I'm guessing it has its uses...
function arrayShuffler(inputArray) [
seedArray = new Array();
targetArray = new Array();
tempArray = new Array();
counter = 0
for (i=0; i 0) [
randNum = parseInt(Math.random() * sourceArray.length);
targetArray[ counter] = seedArray[ randNum];
seedArray.splice(randNum,1);
tempArray[ counter] = inputArray[ targetArray[ counter]];
counter++;
]
return tempArray;
]
Then to use it later on...
array_toBeShuffled = arrayShuffler(array_toBeShuffled);
Hopefully I didn't break it by changing it for this post. (Some variable names changed around for clarity and adding a space to the square brackets so
they don't parse as forum code.) If you couldn't figure it out, it randomizes the order of any array. I'm sure a list of non-repeating randomized
values has some uses. (Card games, quiz questions, other more creative things, etc.)
As for a method to the madness? I find using a loop to populate a seed array and then randomly taking parts of the seed array away to generate another
array which in turn is used to reorder the array put into the function is a lot faster than performing comparison checks to see that a random value
doesn't repeat. (The list of values to randomly choose from gets shorter each time it repeats. As where checking the whole thing would keep bumping
into the same values over and over again if you didn't want a repeat.) I don't do much programming, so it took me a while to figure that one
out.
edit on 17-3-2012 by pauljs75 because: (no reason given)