Created
June 13, 2017 18:36
-
-
Save mllopart/851005f85d7f77a2a08d5fc3c7ed07c3 to your computer and use it in GitHub Desktop.
Random numbers in an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*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