- Download or Clone this repository
- Import the js file to your site
<script src="indexShuffler.js"></script>
The method:
function generateShuffleIndexes(numberOfIndexes){
var randomIndexes = new Array();
//Initialize the first value of the array
randomIndexes[0] = Math.floor((Math.random() * numberOfIndexes));
....
}
function ShuffleArray(array){
var shuffleIndexes = generateShuffleIndexes(array.length);
var shuffledArray = new Array();
for(var i = 0; i <= array.length - 1; i++){
shuffledArray[i] = array[shuffleIndexes[i]];
}
return shuffledArray;
}
Implementation:
console.log(generateShuffleIndexes(20));
Possible Output:
0:2
1:15
2:17
3:6
4:1
5:10
6:9
7:11
8:13
9:19
10:18
11:14
12:4
13:5
14:12
15:16
16:8
17:7
18:0
19:3
var myArray = ["Array1", "Array2", "Array3", "Array4", "Array5"];
console.log(ShuffleArray(myArray));
Possible Output:
0:"Array2"
1:"Array1"
2:"Array4"
3:"Array5"
4:"Array3"