Name changed for easy to find.
This commit is contained in:
14
snippets/randomIntArrayInRange.md
Normal file
14
snippets/randomIntArrayInRange.md
Normal 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(10, 12, 35); // [ 15, 13, 25, 19, 13, 22, 19, 12, 27, 19 ]
|
||||
```
|
||||
@ -1,21 +0,0 @@
|
||||
### 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>
|
||||
@ -140,7 +140,7 @@ pullAtIndex:array
|
||||
pullAtValue:array
|
||||
randomHexColorCode:utility,random
|
||||
randomIntegerInRange:math,utility,random
|
||||
randomIntWithLengthInRange:math,utility,random
|
||||
randomIntArrayInRange:math,utility,random
|
||||
randomNumberInRange:math,utility,random
|
||||
readFileLines:node,array,string
|
||||
redirect:browser,url
|
||||
|
||||
2
test/randomIntArrayInRange/randomIntArrayInRange.js
Normal file
2
test/randomIntArrayInRange/randomIntArrayInRange.js
Normal file
@ -0,0 +1,2 @@
|
||||
module.exports = randomIntArrayInRange = (min, max, n = 1) =>
|
||||
Array.from({ length: n }, () => Math.floor(Math.random() * (max - min + 1)) + min);
|
||||
13
test/randomIntArrayInRange/randomIntArrayInRange.test.js
Normal file
13
test/randomIntArrayInRange/randomIntArrayInRange.test.js
Normal file
@ -0,0 +1,13 @@
|
||||
const test = require('tape');
|
||||
const randomIntArrayInRange = require('./randomIntArrayInRange.js');
|
||||
|
||||
test('Testing randomIntArrayInRange', (t) => {
|
||||
//For more information on all the methods supported by tape
|
||||
//Please go to https://github.com/substack/tape
|
||||
t.true(typeof randomIntArrayInRange === 'function', 'randomIntArrayInRange is a Function');
|
||||
//t.deepEqual(randomIntArrayInRange(args..), 'Expected');
|
||||
//t.equal(randomIntArrayInRange(args..), 'Expected');
|
||||
//t.false(randomIntArrayInRange(args..), 'Expected');
|
||||
//t.throws(randomIntArrayInRange(args..), 'Expected');
|
||||
t.end();
|
||||
});
|
||||
@ -1,4 +0,0 @@
|
||||
module.exports = randomIntWithLengthInRange = (len, min, max) =>
|
||||
Array.from({ length: len }, () =>
|
||||
Math.floor(Math.random() * (max - min) + min)
|
||||
);
|
||||
@ -1,13 +0,0 @@
|
||||
const test = require('tape');
|
||||
const randomIntWithLengthInRange = require('./randomIntWithLengthInRange.js');
|
||||
|
||||
test('Testing randomIntWithLengthInRange', (t) => {
|
||||
//For more information on all the methods supported by tape
|
||||
//Please go to https://github.com/substack/tape
|
||||
t.true(typeof randomIntWithLengthInRange === 'function', 'randomIntWithLengthInRange is a Function');
|
||||
//t.deepEqual(randomIntWithLengthInRange(args..), 'Expected');
|
||||
//t.equal(randomIntWithLengthInRange(args..), 'Expected');
|
||||
//t.false(randomIntWithLengthInRange(args..), 'Expected');
|
||||
//t.throws(randomIntWithLengthInRange(args..), 'Expected');
|
||||
t.end();
|
||||
});
|
||||
Reference in New Issue
Block a user