2017-04-22 22:49:49 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
const make = require('../make')
|
|
|
|
const random = require('../random')
|
|
|
|
|
|
|
|
class number extends make {
|
|
|
|
static bool () {
|
2017-04-25 22:21:31 +00:00
|
|
|
return random.bool()
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static float () {
|
2017-04-25 22:21:31 +00:00
|
|
|
let n
|
2017-04-22 22:49:49 +00:00
|
|
|
if (random.chance(32)) {
|
|
|
|
switch (random.number(4)) {
|
|
|
|
case 0:
|
2017-06-23 15:36:33 +00:00
|
|
|
n = random.range(Number.MIN_VALUE, Number.MAX_VALUE)
|
2017-04-25 22:21:31 +00:00
|
|
|
break
|
2017-04-22 22:49:49 +00:00
|
|
|
case 1:
|
2017-04-25 22:21:31 +00:00
|
|
|
n = Math.pow(10, 1) / Math.pow(10, random.number(307))
|
|
|
|
break
|
2017-04-22 22:49:49 +00:00
|
|
|
case 2:
|
2017-04-25 22:21:31 +00:00
|
|
|
n = Math.pow(2, random.float() * random.float() * 64)
|
|
|
|
break
|
2017-04-22 22:49:49 +00:00
|
|
|
case 3:
|
2017-04-25 22:21:31 +00:00
|
|
|
n = Math.pow(10, random.range(1, 9)) / Math.pow(10, random.range(1, 9))
|
|
|
|
break
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return n
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
switch (random.number(6)) {
|
|
|
|
default:
|
2017-04-25 22:21:31 +00:00
|
|
|
n = random.float()
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return n
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static range () {
|
|
|
|
return random.pick([1, 2, 3, 4, 6, 8, 16, 32, 64, number.tiny])
|
|
|
|
}
|
|
|
|
|
|
|
|
static frange (min, max, precision) {
|
2018-01-16 17:39:48 +00:00
|
|
|
let x = Math.random() * (min - max) + max
|
|
|
|
if (precision) {
|
|
|
|
let power = Math.pow(10, precision || 0)
|
|
|
|
x = Math.round(x * power) / power
|
|
|
|
}
|
|
|
|
return x
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static tiny () {
|
2017-04-25 22:21:31 +00:00
|
|
|
return Math.pow(2, random.number(12))
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned () {
|
2017-04-22 22:49:49 +00:00
|
|
|
if (random.chance(2)) {
|
2018-03-19 03:23:05 +00:00
|
|
|
return Math.abs(number.any())
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-07-20 19:30:11 +00:00
|
|
|
return Math.pow(2, random.number(random.number(65))) + random.number(3) - 1
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 04:54:40 +00:00
|
|
|
static even (number) {
|
2017-04-25 22:21:31 +00:00
|
|
|
return number % 2 === 1 ? ++number : number
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static interesting () {
|
2018-02-12 18:33:54 +00:00
|
|
|
return random.choose([
|
|
|
|
[10, [-128, -1, 0, 1, 16, 32, 64, 100, 127]],
|
|
|
|
[7, [-32768, -129, 128, 255, 256, 512, 1000, 1024, 4096, 32767]],
|
|
|
|
[1, [-2147483648, -100663046, -32769, 32768, 65535, 65536, 100663045, 2147483647]]
|
|
|
|
])
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static any () {
|
2017-04-22 22:49:49 +00:00
|
|
|
let value = random.choose([
|
2018-05-09 15:46:20 +00:00
|
|
|
[1, number.float],
|
|
|
|
[1, number.unsigned],
|
|
|
|
[1, [number.range, number.tiny]]
|
2017-04-25 22:21:31 +00:00
|
|
|
])
|
|
|
|
return random.chance(10) ? -value : value
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
|
|
|
|
module.exports = number
|