19 lines
439 B
Markdown
19 lines
439 B
Markdown
---
|
|
title: randomNumberInRange
|
|
tags: math,random,beginner
|
|
firstSeen: 2017-12-17T17:55:51+02:00
|
|
lastUpdated: 2020-10-22T20:24:04+03:00
|
|
---
|
|
|
|
Generates a random number in the specified range.
|
|
|
|
- Use `Math.random()` to generate a random value, map it to the desired range using multiplication.
|
|
|
|
```js
|
|
const randomNumberInRange = (min, max) => Math.random() * (max - min) + min;
|
|
```
|
|
|
|
```js
|
|
randomNumberInRange(2, 10); // 6.0211363285087005
|
|
```
|