Skip to content

Instantly share code, notes, and snippets.

@ruslansavenok
Created October 8, 2013 12:54
Show Gist options
  • Save ruslansavenok/6884232 to your computer and use it in GitHub Desktop.
Save ruslansavenok/6884232 to your computer and use it in GitHub Desktop.
Randomly slice an array and rearrange elements
/*
* Randomly slice an array and rearrange elements
*
* Useful for fake ajax data
*
* jQuery Example:
* $.get("/fake-ajax.json", function (data) {
* renderSomething(data.randomizeData());
* });
*
*/
Array.prototype.randomizeData = function () {
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
var arraySize = this.length;
var startSliceAt = getRandomInt(0, arraySize);
var endSliceAt = getRandomInt(startSliceAt, arraySize);
return this.slice(startSliceAt, endSliceAt).sort(function() {return 0.5 - Math.random()});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment