30-seconds-of-code
Version:
A collection of useful JavaScript snippets.
14 lines (9 loc) • 350 B
Markdown
### randomIntegerInRange
Returns a random integer in the specified range.
Use `Math.random()` to generate a random number and map it to the desired range, using `Math.floor()` to make it an integer.
```js
const randomIntegerInRange = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
```
```js
randomIntegerInRange(0, 5); // 2
```