diff --git a/lib/make/numbers.js b/lib/make/numbers.js index 99de227..cef5d97 100644 --- a/lib/make/numbers.js +++ b/lib/make/numbers.js @@ -7,7 +7,7 @@ const random = require('../random') 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} * @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 * @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 * @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 * @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 * @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 * @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 * @returns {number} */ @@ -113,36 +113,34 @@ class number extends make { return random.range(0, 4294967295) } + /** + * Returns a random floating point number + * @returns {number} + */ static float () { - let n - if (random.chance(32)) { + /* if (random.chance(32)) { switch (random.number(4)) { case 0: - n = random.range(Number.MIN_VALUE, Number.MAX_VALUE) - break + return random.range(Number.MIN_VALUE, Number.MAX_VALUE) case 1: - n = Math.pow(10, 1) / Math.pow(10, random.number(307)) - break + return Math.pow(10, 1) / Math.pow(10, random.number(307)) case 2: - n = Math.pow(2, random.float() * random.float() * 64) - break + return Math.pow(2, random.float() * random.float() * 64) case 3: - n = Math.pow(10, random.range(1, 9)) / Math.pow(10, random.range(1, 9)) - break + return Math.pow(10, random.range(1, 9)) / Math.pow(10, random.range(1, 9)) } - return n - } - switch (random.number(6)) { - default: - n = random.float() - } - return n - } - - static range () { - return random.pick([1, 2, 3, 4, 6, 8, 16, 32, 64, number.tiny]) + } */ + + return random.float() } + /** + * 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) { let x = Math.random() * (min - max) + max if (precision) { @@ -152,21 +150,28 @@ class number extends make { return x } + /** + * Returnss a random power of 2 between 1 and 2048 + * @returns {number} + */ static tiny () { - return Math.pow(2, random.number(12)) - } - - static unsigned () { - if (random.chance(2)) { - return Math.abs(number.any()) - } - return Math.pow(2, random.number(random.number(65))) + random.number(3) - 1 + // Calling random.number twice prefers lower values + return Math.pow(2, random.number(random.number(13))) } + /** + * Returns a random number adjacent to the supplied number + * @param {number} number + * @returns {number} + */ static even (number) { return number % 2 === 1 ? ++number : number } + /** + * Returns a random number that may be interesting + * @returns {number} + */ static interesting () { return random.choose([ [50, [-128, -1, 0, 1, 16, 32, 64, 100, 127]], @@ -175,14 +180,40 @@ class number extends make { ]) } - static any () { - let value = random.choose([ - [1, number.interesting], - [1, number.float], - [1, number.unsigned], - [1, [number.range, number.tiny]] + /** + * Returns a random signed number + * @returns {number} + */ + static signed () { + 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 } }