diff --git a/packages/shared/src/index.js b/packages/shared/src/index.js index 238a80d1..0b2f9050 100644 --- a/packages/shared/src/index.js +++ b/packages/shared/src/index.js @@ -41,6 +41,7 @@ export { default as FuzzyDate } from './utils/FuzzyDate'; export { default as Hooks } from './utils/Hooks'; export { default as IIIF } from './utils/IIIF'; export { default as Map } from './utils/Map'; +export { default as Numbers } from './utils/Numbers'; export { default as ObjectJs } from './utils/Object'; export { default as String } from './utils/String'; export { default as Timer } from './utils/Timer'; diff --git a/packages/shared/src/utils/Numbers.js b/packages/shared/src/utils/Numbers.js new file mode 100644 index 00000000..2338a4b6 --- /dev/null +++ b/packages/shared/src/utils/Numbers.js @@ -0,0 +1,19 @@ +// @flow + +/** + * Returns a random integer between the min (inclusive) and max (exclude) values. + * + * @param min + * @param max + * + * @returns {number} + */ +const getRandomInt = (min, max) => { + const minCeiled = Math.ceil(min); + const maxFloored = Math.floor(max); + return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); +}; + +export default { + getRandomInt +};