Merge pull request #543 from ergenekonyigit/master

[FEATURE] Add randomIntArrayInRange
This commit is contained in:
Angelos Chalaris
2018-01-15 14:01:47 +02:00
committed by GitHub
4 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,14 @@
### randomIntArrayInRange
Returns an array of n random integers in the specified range.
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 randomIntArrayInRange = (min, max, n = 1) =>
Array.from({ length: n }, () => Math.floor(Math.random() * (max - min + 1)) + min);
```
```js
randomIntArrayInRange(12, 35, 10); // [ 34, 14, 27, 17, 30, 27, 20, 26, 21, 14 ]
```