Last active
January 8, 2017 11:30
-
-
Save vincentorback/3928ea743a530e0428a6a56ef86278b2 to your computer and use it in GitHub Desktop.
randomBetween.js
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
/** | |
* Get a random int between two values | |
* @param {Int} min | |
* @param {Int} max | |
* @return {Int} | |
*/ | |
export function randomBetween(min = 0, max = 0) { | |
return Math.floor(Math.random() * (max - min + 1) + min) | |
} | |
/** | |
* Get a random int between two values, except one value. | |
* @param {Int} min | |
* @param {Int} max | |
* @param {Int} except | |
* @return {Int} | |
*/ | |
export function randomBetweenExcept(min = 0, max = 0, except) { | |
if (min === max) { | |
return min | |
} | |
var num = Math.floor(Math.random() * (max - min + 1) + min) | |
return (num === except) ? randomBetweenExcept(min, max, except) : num | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment