/Index-Shuffler

Return an array of integers as shuffled starting from 0, to be used as indexes as an alternative of Collections.shuffle() which is not supported by JS

Primary LanguageJavaScriptMIT LicenseMIT

Index-Shuffler


Installation

  1. Download or Clone this repository
  2. Import the js file to your site
<script src="indexShuffler.js"></script>	

How to Use?

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:

If you only want to gain shuffled indexes

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

If you want to output the shuffled array

var myArray = ["Array1", "Array2", "Array3", "Array4", "Array5"];
console.log(ShuffleArray(myArray));

Possible Output:

0:"Array2"
1:"Array1"
2:"Array4"
3:"Array5"
4:"Array3"

Check this DEMO

Return an array of integers as shuffled starting from 0, to be used as indexes as an alternative of Collections.shuffle() which is not supported by JS