Skip to content

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

License

Notifications You must be signed in to change notification settings

gicontz/Index-Shuffler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published