@nichathan-gaming/map-generator
Version:
Creates and generates a 2 dimensional array with various path generation functions.
22 lines (18 loc) • 632 B
JavaScript
import random from "./random.js";
/**
* Takes in an array, returns a new array with the elements from the array at random indexes.
* Does not mutate the given array.
*
* @param {Array} array array of elements to shuffle
* @returns a shuffled version of the given array
*/
const shuffle = (array) => {
const shuffledArray = [];
const copiedArray = [...array];
while(copiedArray.length > 0){
const index = Math.floor(random.getNext() * copiedArray.length);
shuffledArray.push(copiedArray.splice(index, 1)[0]);
};
return shuffledArray;
};
export default shuffle;