Skip to content

Instantly share code, notes, and snippets.

@tamalchowdhury
Last active September 4, 2019 14:49
Show Gist options
  • Save tamalchowdhury/6bc8f3bd4ec9e4c8b433b3bee4a895a5 to your computer and use it in GitHub Desktop.
Save tamalchowdhury/6bc8f3bd4ec9e4c8b433b3bee4a895a5 to your computer and use it in GitHub Desktop.
This function takes a list items and randomizes them.
/**
* @param {array} array which is an array of items to be shuffled
* A function which returns an array of random chests like:
* ['gold', 'half', 'silver']
* or
* ['silver', 'gold', 'fifty']
*/
function shuffleArray(array) {
let output = [];
// While we still have items, keep rolling
while (array.length) {
// Pick one random item, fill the array, then remove that item
// Pick a number between 0-2
let random = Math.floor(Math.random() * array.length);
output.push(array[random]);
// Remove the item from the array array
array.splice(random, 1);
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment