diff --git a/package.json b/package.json index 965f2ec..12c72af 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@aureooms/js-random", "description": "Randomness algorithms for JavaScript", - "version": "3.0.0", + "version": "3.1.0", "license": "AGPL-3.0", "author": "Aurélien Ooms ", "homepage": "https://aureooms.github.io/js-random", diff --git a/src/api/randfloat.js b/src/api/randfloat.js index ca14e87..b9909e1 100644 --- a/src/api/randfloat.js +++ b/src/api/randfloat.js @@ -1,7 +1,15 @@ +import random from './random.js'; +import _randfloat from '../kernel/_randfloat.js'; + /** - * Returns a floating point number in interval [i, j[ (i included, j excluded) + * Returns a double in interval [i, j[ (i included, j excluded) * according to a uniform distribution. + * + * @function + * @param {number} i The inclusive left bound + * @param {number} j The non-inclusive right bound + * @return {number} A double in the interval [i, j[ taken uniformly at + * random. */ - -const randfloat = (i, j) => i + Math.random() * (j - i); +const randfloat = _randfloat(random); export default randfloat; diff --git a/src/api/randint.js b/src/api/randint.js index 0a707bb..62168bc 100644 --- a/src/api/randint.js +++ b/src/api/randint.js @@ -1,7 +1,15 @@ +import random from './random.js'; +import _randint from '../kernel/_randint.js'; + /** * Returns an integer in interval [i, j[ (i included, j excluded) * according to a uniform distribution. + * + * @function + * @param {number} i The inclusive left bound + * @param {number} j The non-inclusive right bound + * @return {number} An integer in the interval [i, j[ taken uniformly at + * random. */ - -const randint = (i, j) => i + Math.floor(Math.random() * (j - i)); +const randint = _randint(random); export default randint; diff --git a/src/api/random.js b/src/api/random.js new file mode 100644 index 0000000..46ecbc6 --- /dev/null +++ b/src/api/random.js @@ -0,0 +1,9 @@ +/** + * Returns a double in the interval [0, 1) using JavaScript Math.random(). + * @return {number} + */ +const random = () => { + return Math.random(); +}; + +export default random; diff --git a/src/index.js b/src/index.js index dc477f2..b10c69a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,10 @@ export {default as randfloat} from './api/randfloat.js'; export {default as randint} from './api/randint.js'; +export {default as random} from './api/random.js'; export {default as randrange} from './api/randrange.js'; export {default as sample} from './api/sample.js'; export {default as shuffle} from './api/shuffle.js'; export {default as _fisheryates} from './kernel/_fisheryates.js'; +export {default as _randfloat} from './kernel/_randfloat.js'; +export {default as _randint} from './kernel/_randint.js'; export {default as _shuffle} from './kernel/_shuffle.js'; diff --git a/src/kernel/_randfloat.js b/src/kernel/_randfloat.js new file mode 100644 index 0000000..9403476 --- /dev/null +++ b/src/kernel/_randfloat.js @@ -0,0 +1,23 @@ +/** + * Builds a randfloat function given a random number generator. + * + * @param {Function} random A function taking no arguments that returns a + * double uniformly at random in the interval [0, 1). + * + * @return {Function} A randfloat function. + */ +const _randfloat = (random) => { + /** + * Returns a double in interval [i, j[ (i included, j excluded) + * according to a uniform distribution. + * + * @param {number} i The inclusive left bound + * @param {number} j The non-inclusive right bound + * @return {number} A double in the interval [i, j[ taken uniformly at + * random. + */ + const randfloat = (i, j) => i + random() * (j - i); + return randfloat; +}; + +export default _randfloat; diff --git a/src/kernel/_randint.js b/src/kernel/_randint.js new file mode 100644 index 0000000..ee31451 --- /dev/null +++ b/src/kernel/_randint.js @@ -0,0 +1,23 @@ +/** + * Builds a randint function given a random number generator. + * + * @param {Function} random A function taking no arguments that returns a + * double uniformly at random in the interval [0, 1). + * + * @return {Function} A randint function. + */ +const _randint = (random) => { + /** + * Returns an integer in interval [i, j[ (i included, j excluded) + * according to a uniform distribution. + * + * @param {number} i The inclusive left bound + * @param {number} j The non-inclusive right bound + * @return {number} An integer in the interval [i, j[ taken uniformly at + * random. + */ + const randint = (i, j) => i + Math.floor(random() * (j - i)); + return randint; +}; + +export default _randint;