Further refactoring of make.numbers
This commit is contained in:
parent
ad5ae56561
commit
e611cb8a19
1 changed files with 74 additions and 43 deletions
|
@ -7,7 +7,7 @@ const random = require('../random')
|
||||||
|
|
||||||
class number extends make {
|
class number extends make {
|
||||||
/**
|
/**
|
||||||
* Generate a number that is more likely to exceed the supplied boundary
|
* Returns a number that is more likely to exceed the supplied boundary
|
||||||
* @param value {number}
|
* @param value {number}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
|
@ -27,7 +27,7 @@ class number extends make {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a int8 [-128, 127]
|
* Returns a int8 [-128, 127]
|
||||||
* @param bypass {boolean} - Determines if the range should be exceeded
|
* @param bypass {boolean} - Determines if the range should be exceeded
|
||||||
* @returns {number}
|
* @returns {number}
|
||||||
*/
|
*/
|
||||||
|
@ -43,7 +43,7 @@ class number extends make {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a uint8 [0, 255]
|
* Returns a uint8 [0, 255]
|
||||||
* @param bypass {boolean} - Determines if the range should be exceeded
|
* @param bypass {boolean} - Determines if the range should be exceeded
|
||||||
* @returns {number}
|
* @returns {number}
|
||||||
*/
|
*/
|
||||||
|
@ -56,7 +56,7 @@ class number extends make {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a int16 [-32768, 32767]
|
* Returns a int16 [-32768, 32767]
|
||||||
* @param bypass {boolean} - Determines if the range should be exceeded
|
* @param bypass {boolean} - Determines if the range should be exceeded
|
||||||
* @returns {number}
|
* @returns {number}
|
||||||
*/
|
*/
|
||||||
|
@ -72,7 +72,7 @@ class number extends make {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a uint16 [0, 65535]
|
* Returns a uint16 [0, 65535]
|
||||||
* @param bypass {boolean} - Determines if the range should be exceeded
|
* @param bypass {boolean} - Determines if the range should be exceeded
|
||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
|
@ -85,7 +85,7 @@ class number extends make {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a int32 [-2147483648, 2147483647]
|
* Returns a int32 [-2147483648, 2147483647]
|
||||||
* @param bypass {boolean} - Determines if the range should be exceeded
|
* @param bypass {boolean} - Determines if the range should be exceeded
|
||||||
* @returns {number}
|
* @returns {number}
|
||||||
*/
|
*/
|
||||||
|
@ -101,7 +101,7 @@ class number extends make {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a uint32 [0, 4294967295]
|
* Returns a uint32 [0, 4294967295]
|
||||||
* @param bypass {boolean} - Determines if the range should be exceeded
|
* @param bypass {boolean} - Determines if the range should be exceeded
|
||||||
* @returns {number}
|
* @returns {number}
|
||||||
*/
|
*/
|
||||||
|
@ -113,36 +113,34 @@ class number extends make {
|
||||||
return random.range(0, 4294967295)
|
return random.range(0, 4294967295)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a random floating point number
|
||||||
|
* @returns {number}
|
||||||
|
*/
|
||||||
static float () {
|
static float () {
|
||||||
let n
|
/* if (random.chance(32)) {
|
||||||
if (random.chance(32)) {
|
|
||||||
switch (random.number(4)) {
|
switch (random.number(4)) {
|
||||||
case 0:
|
case 0:
|
||||||
n = random.range(Number.MIN_VALUE, Number.MAX_VALUE)
|
return random.range(Number.MIN_VALUE, Number.MAX_VALUE)
|
||||||
break
|
|
||||||
case 1:
|
case 1:
|
||||||
n = Math.pow(10, 1) / Math.pow(10, random.number(307))
|
return Math.pow(10, 1) / Math.pow(10, random.number(307))
|
||||||
break
|
|
||||||
case 2:
|
case 2:
|
||||||
n = Math.pow(2, random.float() * random.float() * 64)
|
return Math.pow(2, random.float() * random.float() * 64)
|
||||||
break
|
|
||||||
case 3:
|
case 3:
|
||||||
n = Math.pow(10, random.range(1, 9)) / Math.pow(10, random.range(1, 9))
|
return Math.pow(10, random.range(1, 9)) / Math.pow(10, random.range(1, 9))
|
||||||
break
|
|
||||||
}
|
}
|
||||||
return n
|
} */
|
||||||
}
|
|
||||||
switch (random.number(6)) {
|
return random.float()
|
||||||
default:
|
|
||||||
n = random.float()
|
|
||||||
}
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
|
|
||||||
static range () {
|
|
||||||
return random.pick([1, 2, 3, 4, 6, 8, 16, 32, 64, number.tiny])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returnss a float value within the supplied range
|
||||||
|
* @param {number} min - Start value
|
||||||
|
* @param {number} max - End value
|
||||||
|
* @param {number} precision
|
||||||
|
* @returns {number}
|
||||||
|
*/
|
||||||
static frange (min, max, precision) {
|
static frange (min, max, precision) {
|
||||||
let x = Math.random() * (min - max) + max
|
let x = Math.random() * (min - max) + max
|
||||||
if (precision) {
|
if (precision) {
|
||||||
|
@ -152,21 +150,28 @@ class number extends make {
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returnss a random power of 2 between 1 and 2048
|
||||||
|
* @returns {number}
|
||||||
|
*/
|
||||||
static tiny () {
|
static tiny () {
|
||||||
return Math.pow(2, random.number(12))
|
// Calling random.number twice prefers lower values
|
||||||
}
|
return Math.pow(2, random.number(random.number(13)))
|
||||||
|
|
||||||
static unsigned () {
|
|
||||||
if (random.chance(2)) {
|
|
||||||
return Math.abs(number.any())
|
|
||||||
}
|
|
||||||
return Math.pow(2, random.number(random.number(65))) + random.number(3) - 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a random number adjacent to the supplied number
|
||||||
|
* @param {number} number
|
||||||
|
* @returns {number}
|
||||||
|
*/
|
||||||
static even (number) {
|
static even (number) {
|
||||||
return number % 2 === 1 ? ++number : number
|
return number % 2 === 1 ? ++number : number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a random number that may be interesting
|
||||||
|
* @returns {number}
|
||||||
|
*/
|
||||||
static interesting () {
|
static interesting () {
|
||||||
return random.choose([
|
return random.choose([
|
||||||
[50, [-128, -1, 0, 1, 16, 32, 64, 100, 127]],
|
[50, [-128, -1, 0, 1, 16, 32, 64, 100, 127]],
|
||||||
|
@ -175,14 +180,40 @@ class number extends make {
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
static any () {
|
/**
|
||||||
let value = random.choose([
|
* Returns a random signed number
|
||||||
[1, number.interesting],
|
* @returns {number}
|
||||||
[1, number.float],
|
*/
|
||||||
[1, number.unsigned],
|
static signed () {
|
||||||
[1, [number.range, number.tiny]]
|
return random.choose([
|
||||||
|
[50, [number.int8]],
|
||||||
|
[30, [number.int16]],
|
||||||
|
[1, [number.int32]]
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a random unsigned number
|
||||||
|
* @returns {number}
|
||||||
|
*/
|
||||||
|
static unsigned () {
|
||||||
|
return random.choose([
|
||||||
|
[50, [number.uint8]],
|
||||||
|
[30, [number.uint16]],
|
||||||
|
[1, [number.uint32]]
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a random number using the type base number generators above
|
||||||
|
* @returns {number}
|
||||||
|
*/
|
||||||
|
static any () {
|
||||||
|
return random.choose([
|
||||||
|
[50, [number.int8, number.uint8]],
|
||||||
|
[30, [number.int16, number.uint16]],
|
||||||
|
[1, [number.int32, number.uint32]]
|
||||||
])
|
])
|
||||||
return random.chance(10) ? -value : value
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue