Skip to content

Instantly share code, notes, and snippets.

@SimonHoiberg
Created November 14, 2020 14:17
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SimonHoiberg/0dc85e01c7c872c3ddc2a409de1232a3 to your computer and use it in GitHub Desktop.
Save SimonHoiberg/0dc85e01c7c872c3ddc2a409de1232a3 to your computer and use it in GitHub Desktop.
Random number given a range
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