@chancejs/core
Advanced tools
Comparing version 2.2.2 to 2.2.3
@@ -5,14 +5,5 @@ export interface IBoolOptions { | ||
export default class Core { | ||
private random; | ||
random: () => number; | ||
constructor(); | ||
/** | ||
* Return a random bool, either true or false | ||
* | ||
* @param {Object} [options={ likelihood: 50 }] alter the likelihood of | ||
* receiving a true or false value back. | ||
* @throws {RangeError} if the likelihood is out of bounds | ||
* @returns {Bool} either true or false | ||
*/ | ||
bool(options?: IBoolOptions): boolean; | ||
} | ||
//# sourceMappingURL=main.d.ts.map |
@@ -12,26 +12,4 @@ "use strict"; | ||
} | ||
/** | ||
* Return a random bool, either true or false | ||
* | ||
* @param {Object} [options={ likelihood: 50 }] alter the likelihood of | ||
* receiving a true or false value back. | ||
* @throws {RangeError} if the likelihood is out of bounds | ||
* @returns {Bool} either true or false | ||
*/ | ||
bool(options) { | ||
let likelihood; | ||
if (options === undefined) { | ||
likelihood = 50; | ||
} | ||
else { | ||
likelihood = options.likelihood; | ||
} | ||
// testRange( | ||
// options.likelihood < 0 || options.likelihood > 100, | ||
// "Chance: Likelihood accepts values from 0 to 100." | ||
// ) | ||
return this.random() * 100 < likelihood; | ||
} | ||
} | ||
exports.default = Core; | ||
//# sourceMappingURL=main.js.map |
@@ -9,42 +9,10 @@ "use strict"; | ||
const chance = new main_1.default(); | ||
ava_1.default('bool() returns a random boolean', t => { | ||
const bool = chance.bool(); | ||
t.is(typeof bool, 'boolean'); | ||
ava_1.default('chance() returns a random number', t => { | ||
t.is(typeof chance.random(), 'number'); | ||
}); | ||
ava_1.default('bool() is within the bounds of what we would call random', t => { | ||
let trueCount = 0; | ||
ava_1.default('chance() returns a random number between 0 and 1', t => { | ||
for (let i = 0; i < 1000; i++) { | ||
if (chance.bool()) { | ||
trueCount++; | ||
} | ||
t.true(chance.random() >= 0 && chance.random() <= 1); | ||
} | ||
// The probability of this test failing is approximately 4.09e-86. | ||
// So, in theory, it could give a false negative, but the sun will | ||
// probably die long before that happens. | ||
t.true((trueCount > 200) && (trueCount < 800)); | ||
}); | ||
ava_1.default('bool() takes and obeys likelihood', t => { | ||
let trueCount = 0; | ||
for (let i = 0; i < 1000; i++) { | ||
if (chance.bool({ likelihood: 30 })) { | ||
trueCount++; | ||
} | ||
} | ||
// Expect it to average around 300 | ||
t.true((trueCount > 200) && (trueCount < 400)); | ||
trueCount = 0; | ||
for (let i = 0; i < 1000; i++) { | ||
if (chance.bool({ likelihood: 99 })) { | ||
trueCount++; | ||
} | ||
} | ||
// Expect it to average at 990 | ||
t.true(trueCount > 900); | ||
}); | ||
// test('bool() throws an error if likelihood < 0 or > 100', t => { | ||
// const fn1 = () => chance.bool({likelihood: -23}) | ||
// t.throws(fn1, RangeError) | ||
// const fn2 = () => chance.bool({likelihood: 7933}) | ||
// t.throws(fn2, RangeError) | ||
// }) | ||
//# sourceMappingURL=main.test.js.map |
{ | ||
"name": "@chancejs/core", | ||
"version": "2.2.2", | ||
"version": "2.2.3", | ||
"description": "ChanceJS core utilities", | ||
@@ -40,3 +40,3 @@ "main": "lib/main.js", | ||
}, | ||
"gitHead": "180510e231c389eae326e98a0ef930785dfd1014" | ||
"gitHead": "52a60d01b278429212a2e538449503c7a841300a" | ||
} |
@@ -6,49 +6,10 @@ import test from 'ava' | ||
test('bool() returns a random boolean', t => { | ||
const bool = chance.bool() | ||
t.is(typeof bool, 'boolean') | ||
test('chance() returns a random number', t => { | ||
t.is(typeof chance.random(), 'number') | ||
}) | ||
test('bool() is within the bounds of what we would call random', t => { | ||
let trueCount = 0 | ||
for (let i=0; i < 1000; i++) { | ||
if (chance.bool()) { | ||
trueCount++ | ||
} | ||
test('chance() returns a random number between 0 and 1', t => { | ||
for (let i = 0; i < 1000; i++) { | ||
t.true(chance.random() >= 0 && chance.random() <= 1) | ||
} | ||
// The probability of this test failing is approximately 4.09e-86. | ||
// So, in theory, it could give a false negative, but the sun will | ||
// probably die long before that happens. | ||
t.true((trueCount > 200) && (trueCount < 800)) | ||
}) | ||
test('bool() takes and obeys likelihood', t => { | ||
let trueCount = 0 | ||
for (let i=0; i < 1000; i++) { | ||
if (chance.bool({ likelihood: 30 })) { | ||
trueCount++ | ||
} | ||
} | ||
// Expect it to average around 300 | ||
t.true((trueCount > 200) && (trueCount < 400)) | ||
trueCount = 0 | ||
for (let i=0; i < 1000; i++) { | ||
if (chance.bool({ likelihood: 99 })) { | ||
trueCount++ | ||
} | ||
} | ||
// Expect it to average at 990 | ||
t.true(trueCount > 900) | ||
}) | ||
// test('bool() throws an error if likelihood < 0 or > 100', t => { | ||
// const fn1 = () => chance.bool({likelihood: -23}) | ||
// t.throws(fn1, RangeError) | ||
// const fn2 = () => chance.bool({likelihood: 7933}) | ||
// t.throws(fn2, RangeError) | ||
// }) |
@@ -8,3 +8,3 @@ import MersenneTwister from '@chancejs/mersenne-twister' | ||
export default class Core { | ||
private random: () => number | ||
public random: () => number | ||
@@ -15,26 +15,2 @@ constructor() { | ||
} | ||
/** | ||
* Return a random bool, either true or false | ||
* | ||
* @param {Object} [options={ likelihood: 50 }] alter the likelihood of | ||
* receiving a true or false value back. | ||
* @throws {RangeError} if the likelihood is out of bounds | ||
* @returns {Bool} either true or false | ||
*/ | ||
public bool(options?: IBoolOptions): boolean { | ||
let likelihood: number | ||
if (options === undefined) { | ||
likelihood = 50 | ||
} else { | ||
likelihood = options.likelihood | ||
} | ||
// testRange( | ||
// options.likelihood < 0 || options.likelihood > 100, | ||
// "Chance: Likelihood accepts values from 0 to 100." | ||
// ) | ||
return this.random() * 100 < likelihood | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
98840
69