Skip to content

Instantly share code, notes, and snippets.

@avisek
Created October 7, 2018 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avisek/b0fa7d16fea08878d2707df583a1d164 to your computer and use it in GitHub Desktop.
Save avisek/b0fa7d16fea08878d2707df583a1d164 to your computer and use it in GitHub Desktop.
// Getting a random number between 0 (inclusive) and 1 (exclusive)
function getRandom() {
return Math.random();
}
// Getting a random number between two values
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
// Getting a random integer between two values
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}
// Getting a random integer between two values, inclusive
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment