-
-
Save SimonHoiberg/0dc85e01c7c872c3ddc2a409de1232a3 to your computer and use it in GitHub Desktop.
Random number given a range
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
const randomNumber = ({ min, max } = { min: 0, max: 1 }) => { | |
if (min >= max) { | |
throw Error( | |
`minimum value (${min}) is larger than or equal to maximum value (${max})` | |
); | |
} | |
return Math.floor(Math.random() * Math.floor(max - min + 1) + min); | |
}; | |
// Usage: random number between 10 and 100. | |
const n = randomNumber({ min: 10, max: 100 }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment