Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8440ee9

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

File tree

Expand file treeCollapse file tree

6 files changed

+79
-5
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+79
-5
lines changed

‎src/api/randfloat.js

Copy file name to clipboard
+11-3Lines changed: 11 additions & 3 deletions
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

Copy file name to clipboard
+10-2Lines changed: 10 additions & 2 deletions
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

Copy file name to clipboard
+9Lines changed: 9 additions & 0 deletions
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

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
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

Copy file name to clipboard
+23Lines changed: 23 additions & 0 deletions
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

Copy file name to clipboard
+23Lines changed: 23 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.