octo-deno/lib/make/numbers.ts

219 lines
5.2 KiB
TypeScript
Raw Permalink Normal View History

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/. */
2020-02-04 12:49:42 +00:00
import random from "../random/random.ts";
2020-02-04 12:49:42 +00:00
export default class numbers {
2018-08-02 18:21:27 +00:00
/**
2018-08-20 16:22:16 +00:00
* Returns a number that is more likely to exceed the supplied boundary
* @param value {number}
2018-08-02 18:21:27 +00:00
* @private
*/
static _exceed (value) {
switch (random.number(4)) {
case 0:
// Divisions
return Math.ceil(value / random.range(2, 4))
case 1:
// Powers
const offset = Math.pow(2, random.range(1, 7))
return (value > 0) ? (value - offset) : value + offset
default:
// Slightly less than limit
return (value > 0) ? (value - random.number(3)) : value + random.number(3)
}
2018-08-02 18:21:27 +00:00
}
/**
2018-08-20 16:22:16 +00:00
* Returns a int8 [-128, 127]
2018-08-02 18:21:27 +00:00
* @param bypass {boolean} - Determines if the range should be exceeded
* @returns {number}
*/
static int8 (bypass = false) {
if (bypass || random.chance(50)) {
2020-02-04 12:49:42 +00:00
return numbers._exceed(random.choose([
[1, -128],
[10, 127]
]))
2018-08-02 18:21:27 +00:00
}
return random.range(-128, 127)
}
/**
2018-08-20 16:22:16 +00:00
* Returns a uint8 [0, 255]
2018-08-02 18:21:27 +00:00
* @param bypass {boolean} - Determines if the range should be exceeded
* @returns {number}
*/
static uint8 (bypass = false) {
if (bypass || random.chance(50)) {
2020-02-04 12:49:42 +00:00
return numbers._exceed(255)
2018-08-02 18:21:27 +00:00
}
return random.range(0, 255)
}
/**
2018-08-20 16:22:16 +00:00
* Returns a int16 [-32768, 32767]
2018-08-02 18:21:27 +00:00
* @param bypass {boolean} - Determines if the range should be exceeded
* @returns {number}
*/
static int16 (bypass = false) {
if (bypass || random.chance(50)) {
2020-02-04 12:49:42 +00:00
return numbers._exceed(random.choose([
[1, -32768],
[10, 32767]
]))
2018-08-02 18:21:27 +00:00
}
return random.range(-32768, 32767)
}
/**
2018-08-20 16:22:16 +00:00
* Returns a uint16 [0, 65535]
2018-08-02 18:21:27 +00:00
* @param bypass {boolean} - Determines if the range should be exceeded
* @returns {*}
*/
static uint16 (bypass = false) {
if (bypass || random.chance(50)) {
2020-02-04 12:49:42 +00:00
return numbers._exceed(65535)
2018-08-02 18:21:27 +00:00
}
return random.range(-0, 65535)
}
/**
2018-08-20 16:22:16 +00:00
* Returns a int32 [-2147483648, 2147483647]
2018-08-02 18:21:27 +00:00
* @param bypass {boolean} - Determines if the range should be exceeded
* @returns {number}
*/
static int32 (bypass = false) {
if (bypass || random.chance(50)) {
2020-02-04 12:49:42 +00:00
return numbers._exceed(random.choose([
[1, -2147483648],
[10, 2147483647]
]))
2018-08-02 18:21:27 +00:00
}
return random.range(-2147483648, 2147483647)
}
/**
2018-08-20 16:22:16 +00:00
* Returns a uint32 [0, 4294967295]
2018-08-02 18:21:27 +00:00
* @param bypass {boolean} - Determines if the range should be exceeded
* @returns {number}
*/
static uint32 (bypass = false) {
if (bypass || random.chance(50)) {
2020-02-04 12:49:42 +00:00
return numbers._exceed(4294967295)
2018-08-02 18:21:27 +00:00
}
return random.range(0, 4294967295)
}
2018-08-20 16:22:16 +00:00
/**
* Returns a random floating point number
* @returns {number}
*/
static float () {
2018-08-20 16:22:16 +00:00
/* if (random.chance(32)) {
2017-04-22 22:49:49 +00:00
switch (random.number(4)) {
case 0:
2018-08-20 16:22:16 +00:00
return random.range(Number.MIN_VALUE, Number.MAX_VALUE)
2017-04-22 22:49:49 +00:00
case 1:
2018-08-20 16:22:16 +00:00
return Math.pow(10, 1) / Math.pow(10, random.number(307))
2017-04-22 22:49:49 +00:00
case 2:
2018-08-20 16:22:16 +00:00
return Math.pow(2, random.float() * random.float() * 64)
2017-04-22 22:49:49 +00:00
case 3:
2018-08-20 16:22:16 +00:00
return Math.pow(10, random.range(1, 9)) / Math.pow(10, random.range(1, 9))
2017-04-22 22:49:49 +00:00
}
2018-08-20 16:22:16 +00:00
} */
2018-08-20 16:22:16 +00:00
return random.float()
}
2018-08-20 16:22:16 +00:00
/**
2018-08-20 16:54:21 +00:00
* Returns a float value within the supplied range
2018-08-20 16:22:16 +00:00
* @param {number} min - Start value
* @param {number} max - End value
2018-09-13 14:39:46 +00:00
* @param {?number} precision
2018-08-20 16:22:16 +00:00
* @returns {number}
*/
2018-09-13 14:39:46 +00:00
static frange (min, max, precision = null) {
let x = Math.random() * (min - max) + max
if (precision) {
2018-09-13 14:39:46 +00:00
let power = Math.pow(10, precision)
x = Math.round(x * power) / power
}
return x
}
2018-08-20 16:22:16 +00:00
/**
2018-08-20 16:54:21 +00:00
* Returns a random power of 2 between 1 and 2048
2018-08-20 16:22:16 +00:00
* @returns {number}
*/
static tiny () {
2018-08-20 16:22:16 +00:00
// Calling random.number twice prefers lower values
return Math.pow(2, random.number(random.number(13)))
}
2018-08-20 16:22:16 +00:00
/**
* Returns a random number adjacent to the supplied number
2020-02-04 12:49:42 +00:00
* @param {num} number
* @returns {num}
2018-08-20 16:22:16 +00:00
*/
2020-02-04 12:49:42 +00:00
static even (num) {
return num % 2 === 1 ? ++num : num
}
2018-08-20 16:22:16 +00:00
/**
* Returns a random number that may be interesting
* @returns {number}
*/
static interesting () {
2018-02-12 18:33:54 +00:00
return random.choose([
[50, [-128, -1, 0, 1, 16, 32, 64, 100, 127]],
[30, [-32768, -129, 128, 255, 256, 512, 1000, 1024, 4096, 32767]],
2018-02-12 18:33:54 +00:00
[1, [-2147483648, -100663046, -32769, 32768, 65535, 65536, 100663045, 2147483647]]
])
}
2018-08-20 16:22:16 +00:00
/**
* Returns a random signed number
* @returns {number}
*/
static signed () {
return random.choose([
2020-02-04 12:49:42 +00:00
[50, [numbers.int8]],
[30, [numbers.int16]],
[1, [numbers.int32]]
2018-08-20 16:22:16 +00:00
])
}
/**
* Returns a random unsigned number
* @returns {number}
*/
static unsigned () {
return random.choose([
2020-02-04 12:49:42 +00:00
[50, [numbers.uint8]],
[30, [numbers.uint16]],
[1, [numbers.uint32]]
2018-08-20 16:22:16 +00:00
])
}
/**
* Returns a random number using the type base number generators above
* @returns {number}
*/
static any () {
2018-08-20 16:22:16 +00:00
return random.choose([
2020-02-04 12:49:42 +00:00
[50, [numbers.int8, numbers.uint8]],
[30, [numbers.int16, numbers.uint16]],
[1, [numbers.int32, numbers.uint32]]
])
2017-04-22 22:49:49 +00:00
}
}