Created
January 21, 2019 02:38
-
-
Save ezirmusitua/6e62ffcfa082e471182559a5e1dcd876 to your computer and use it in GitHub Desktop.
[Generate random number] generate random integer arbitrary #math #random #javascript #node
This file contains hidden or 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
// Reference: https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range | |
/** | |
* Returns a random number between min (inclusive) and max (exclusive) | |
*/ | |
function getRandomArbitrary(min, max) { | |
return Math.random() * (max - min) + min; | |
} | |
/** | |
* Returns a random integer between min (inclusive) and max (inclusive). | |
* The value is no lower than min (or the next integer greater than min | |
* if min isn't an integer) and no greater than max (or the next integer | |
* lower than max if max isn't an integer). | |
* Using Math.round() will give you a non-uniform distribution! | |
*/ | |
function getRandomInt(min, max) { | |
min = Math.ceil(min); | |
max = Math.floor(max); | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment