Skip to content

Instantly share code, notes, and snippets.

@TheGreatRambler
Last active February 15, 2018 17:12
Show Gist options
  • Select an option

  • Save TheGreatRambler/3e1685e470c4f4b820301559ecf29b56 to your computer and use it in GitHub Desktop.

Select an option

Save TheGreatRambler/3e1685e470c4f4b820301559ecf29b56 to your computer and use it in GitHub Desktop.
Use this function to randomly ruin a array by splitting it up and inserting those chunks in random places.
function randomlySplitArray(inputarray, chunksize, numofloops) {
function randomint(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var temparray = inputarray;
for (var q = 0; q < numofloops; q++) {
var start = randomint(0, inputarray.length - chunksize);
var othertemp = temparray.splice(start, chunksize);
var index = randomint(0, temparray.length - chunksize - 1);
// https://stackoverflow.com/questions/7032550/javascript-insert-an-array-inside-another-array
temparray = temparray.slice(0, index).concat(othertemp).concat(temparray.slice(index));
}
return temparray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment