|
java script random array shufler
hi I'm just new to java and I'm trying to write a script that will shuffle the information in a array with no gaps and no repeats. below is that code that i have so far. thanks ben
[CODE]
//constants
SWAPS_TO_BE_MADE = 30;
//variables
var myArray = new Array('Ace',2,3,4,5,6,7,8,9,10,'Jack','Queen','King'); // the array that entities will be shuffled from
var temp = 0;
var outPutArray = new Array (12);
var arrayLength = myArray.length;
var index = 0;
var swapIndex = 0;
var self_swaps = 0;
var counter = 0;
temp=myArray[swapIndex]
document.write('<h1>Array shuffling</h1>');
document.write('<p>Before shuffle: '+myArray+'</p>');
//loop
while(counter < SWAPS_TO_BE_MADE) {
index =Math.floor(Math.random()*arrayLength);
swapIndex =Math.floor(Math.random()*arrayLength);
var index_value = index;
var range = 1;
myArray.splice(index_value,range,'spilced');
outPutArray[swapIndex] = index;
if(index == swapIndex) {
self_swaps++;
}
counter++;
index = (index + 1 % arrayLength);
swapIndex= (swapIndex + 1 % arrayLength);
}
document.write('<p>After shuffle: '+outPutArray+'</p>');
document.write('<p>After shuffle: '+myArray+'</p>');
[ICODE]
|