Skip to content

Commit 58a7c75

Browse files
✨ feat(randrange): Export kernel constructor _randrange.
1 parent 8c70519 commit 58a7c75

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

src/api/randrange.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _randrange from '../kernel/_randrange.js';
12
import randint from './randint.js';
23

34
/**
@@ -13,20 +14,11 @@ import randint from './randint.js';
1314
*
1415
* TODO: Handle empty ranges.
1516
*
17+
* @function
1618
* @param {number} [start=0] - The starting value.
1719
* @param {number} stop - The stopping value.
1820
* @param {number} [step=1] - The step value.
1921
* @return {number} The picked element.
2022
*/
21-
const randrange = (start, stop, step) => {
22-
if (stop === undefined) return randint(0, start);
23-
if (step === undefined) step = 1;
24-
25-
if (stop >= start) {
26-
return start + step * randint(0, Math.floor((stop - start) / step));
27-
}
28-
29-
return start + step * randint(0, Math.floor((start - stop) / -step));
30-
};
31-
23+
const randrange = _randrange(randint);
3224
export default randrange;

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ export {default as _fisheryates} from './kernel/_fisheryates.js';
1212
export {default as _fisheryates_inside_out} from './kernel/_fisheryates_inside_out.js';
1313
export {default as _randfloat} from './kernel/_randfloat.js';
1414
export {default as _randint} from './kernel/_randint.js';
15+
export {default as _randrange} from './kernel/_randrange.js';
1516
export {default as _shuffle} from './kernel/_shuffle.js';
1617
export {default as _waterman} from './kernel/_waterman.js';

src/kernel/_randrange.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Builds a randrange function given a randint function.
3+
*
4+
* @param {Function} randint The randint function.
5+
* @return {Function} The choice function.
6+
*/
7+
const _randrange = (randint) => {
8+
/**
9+
* Pick an element from range(start, stop, step) uniformly at random.
10+
*
11+
* Return an element from range(start, stop, step) selected uniformly at random.
12+
* If step is positive, this set corresponds to
13+
* {x: x in [start, stop[ AND x % step = 0}.
14+
* If step is negative, the range has to be given in reverse order, that is,
15+
* largest value first, smallest value second. Both the starting value and the
16+
* step value are optional. By default the starting value is <code>0</code>.
17+
* The default for the step value is <code>1</code>.
18+
*
19+
* TODO: Handle empty ranges.
20+
*
21+
* @param {number} [start=0] - The starting value.
22+
* @param {number} stop - The stopping value.
23+
* @param {number} [step=1] - The step value.
24+
* @return {number} The picked element.
25+
*/
26+
const randrange = (start, stop, step) => {
27+
if (stop === undefined) return randint(0, start);
28+
if (step === undefined) step = 1;
29+
30+
if (stop >= start) {
31+
return start + step * randint(0, Math.floor((stop - start) / step));
32+
}
33+
34+
return start + step * randint(0, Math.floor((start - stop) / -step));
35+
};
36+
37+
return randrange;
38+
};
39+
40+
export default _randrange;

0 commit comments

Comments
 (0)