randomIntWithLengthInRange

This commit is contained in:
ergenekonyigit
2018-01-13 20:13:37 +03:00
parent bdbc6db294
commit 3c69263268
4 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,23 @@
### randomIntWithLengthInRange
Returns a random integer in the specified range and length.
Use `Array.from()` to create an empty array of the specific length. `Math.random()` to generate a random number and map it to the desired range, using `Math.floor()` to make it an integer.
```js
const randomIntWithLengthInRange = (len, min, max) =>
Array.from({ length: len }, () =>
Math.floor(Math.random() * (max - min) + min)
);
```
<details>
<summary>Examples</summary>
```js
randomIntWithLengthInRange(10, 12, 35); // [ 14, 20, 12, 29, 23, 16, 26, 22, 15, 24 ]
```
</details>
<br>[⬆ Back to top](#table-of-contents)