Skip to content

Commit 8440ee9

Browse files
✨ feat(api): Expose random, _randint, and randfloat.
Fixes #89.
1 parent fa49309 commit 8440ee9

File tree

6 files changed

+79
-5
lines changed

6 files changed

+79
-5
lines changed

src/api/randfloat.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
import random from './random.js';
2+
import _randfloat from '../kernel/_randfloat.js';
3+
14
/**
2-
* Returns a floating point number in interval [i, j[ (i included, j excluded)
5+
* Returns a double in interval [i, j[ (i included, j excluded)
36
* according to a uniform distribution.
7+
*
8+
* @function
9+
* @param {number} i The inclusive left bound
10+
* @param {number} j The non-inclusive right bound
11+
* @return {number} A double in the interval [i, j[ taken uniformly at
12+
* random.
413
*/
5-
6-
const randfloat = (i, j) => i + Math.random() * (j - i);
14+
const randfloat = _randfloat(random);
715
export default randfloat;

src/api/randint.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
import random from './random.js';
2+
import _randint from '../kernel/_randint.js';
3+
14
/**
25
* Returns an integer in interval [i, j[ (i included, j excluded)
36
* according to a uniform distribution.
7+
*
8+
* @function
9+
* @param {number} i The inclusive left bound
10+
* @param {number} j The non-inclusive right bound
11+
* @return {number} An integer in the interval [i, j[ taken uniformly at
12+
* random.
413
*/
5-
6-
const randint = (i, j) => i + Math.floor(Math.random() * (j - i));
14+
const randint = _randint(random);
715
export default randint;

src/api/random.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Returns a double in the interval [0, 1) using JavaScript Math.random().
3+
* @return {number}
4+
*/
5+
const random = () => {
6+
return Math.random();
7+
};
8+
9+
export default random;

src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
export {default as randfloat} from './api/randfloat.js';
22
export {default as randint} from './api/randint.js';
3+
export {default as random} from './api/random.js';
34
export {default as randrange} from './api/randrange.js';
45
export {default as sample} from './api/sample.js';
56
export {default as shuffle} from './api/shuffle.js';
67
export {default as _fisheryates} from './kernel/_fisheryates.js';
8+
export {default as _randfloat} from './kernel/_randfloat.js';
9+
export {default as _randint} from './kernel/_randint.js';
710
export {default as _shuffle} from './kernel/_shuffle.js';

src/kernel/_randfloat.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Builds a randfloat function given a random number generator.
3+
*
4+
* @param {Function} random A function taking no arguments that returns a
5+
* double uniformly at random in the interval [0, 1).
6+
*
7+
* @return {Function} A randfloat function.
8+
*/
9+
const _randfloat = (random) => {
10+
/**
11+
* Returns a double in interval [i, j[ (i included, j excluded)
12+
* according to a uniform distribution.
13+
*
14+
* @param {number} i The inclusive left bound
15+
* @param {number} j The non-inclusive right bound
16+
* @return {number} A double in the interval [i, j[ taken uniformly at
17+
* random.
18+
*/
19+
const randfloat = (i, j) => i + random() * (j - i);
20+
return randfloat;
21+
};
22+
23+
export default _randfloat;

src/kernel/_randint.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Builds a randint function given a random number generator.
3+
*
4+
* @param {Function} random A function taking no arguments that returns a
5+
* double uniformly at random in the interval [0, 1).
6+
*
7+
* @return {Function} A randint function.
8+
*/
9+
const _randint = (random) => {
10+
/**
11+
* Returns an integer in interval [i, j[ (i included, j excluded)
12+
* according to a uniform distribution.
13+
*
14+
* @param {number} i The inclusive left bound
15+
* @param {number} j The non-inclusive right bound
16+
* @return {number} An integer in the interval [i, j[ taken uniformly at
17+
* random.
18+
*/
19+
const randint = (i, j) => i + Math.floor(random() * (j - i));
20+
return randint;
21+
};
22+
23+
export default _randint;

0 commit comments

Comments
 (0)