Skip to content

Instantly share code, notes, and snippets.

@mllopart
Created June 13, 2017 18:36
Show Gist options
  • Save mllopart/851005f85d7f77a2a08d5fc3c7ed07c3 to your computer and use it in GitHub Desktop.
Save mllopart/851005f85d7f77a2a08d5fc3c7ed07c3 to your computer and use it in GitHub Desktop.
Random numbers in an array
/*Function to return an array containing *length* random numbers
and having the range from 0 to *max* */
function randomArray(length, max) {
return Array.apply(null, Array(length)).map(function() {
return Math.round(Math.random() * max);
});
}
/* function than shuffle an array */
function shuffle(array) {
var tmp, current, top = array.length;
if(top) while(--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array;
}
resultArray = randomArray(20,500);
document.write(resultArray);
document.write("\n");
document.write(shuffle(resultArray));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment