Files

45 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2026-11-17 16:15:01 -05:00
void function(root){
function defaults(options){
var options = options || {}
var min = options.min
var max = options.max
var integer = options.integer || false
if ( min == null && max == null ) {
min = 0
max = 1
} else if ( min == null ) {
min = max - 1
} else if ( max == null ) {
max = min + 1
}
if ( max < min ) throw new Error('invalid options, max must be >= min')
return {
min: min
, max: max
, integer: integer
}
}
function random(options){
options = defaults(options)
if ( options.max === options.min ) return options.min
var r = Math.random() * (options.max - options.min + Number(!!options.integer)) + options.min
return options.integer ? Math.floor(r) : r
}
function generator(options){
options = defaults(options)
return function(min, max, integer){
options.min = min != null ? min : options.min
options.max = max != null ? max : options.max
options.integer = integer != null ? integer : options.integer
return random(options)
}
}
module.exports = random
module.exports.generator = generator
module.exports.defaults = defaults
}(this)