@dillonkearns/elm-graphql
Version:
<img src="https://cdn.jsdelivr.net/gh/martimatix/logo-graphqelm/logo.svg" alt="dillonearns/elm-graphql logo" width="40%" align="right">
49 lines (41 loc) • 1.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.RANDOM = undefined;
exports.random = random;
exports.randomInt = randomInt;
exports.randomRange = randomRange;
exports.randomBool = randomBool;
var _Eff = require('./Eff');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
// The `RANDOM` effect indicates that an Eff action may access or modify the
// JavaScript global random number generator, i.e. `Math.random()`.
var RANDOM = exports.RANDOM = function RANDOM() {
_classCallCheck(this, RANDOM);
};
function random() {
return (0, _Eff.inj)(function () {
return Math.random();
});
}
// Returns a random integer between min (included) and max (included)
function randomInt(min, max) {
var lo = Math.ceil(min);
var hi = Math.floor(max);
return (0, _Eff.map)(function (n) {
return Math.floor(n * (hi - lo + 1)) + lo;
}, random());
}
// Returns a random number between a minimum value (inclusive) and a maximum value (exclusive)
function randomRange(min, max) {
return (0, _Eff.map)(function (n) {
return Math.floor(n * (max - min)) + min;
}, random());
}
// Returns a random boolean value with an equal chance of being `true` or `false`
function randomBool() {
return (0, _Eff.map)(function (n) {
return n < 0.5;
}, random());
}