Skip to content

Commit

Permalink
rand now supports decimals, tests updated
Browse files Browse the repository at this point in the history
  • Loading branch information
user24 committed May 6, 2020
1 parent cf4fdac commit 542e7db
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 25 deletions.
81 changes: 58 additions & 23 deletions src/random.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,72 @@ import { randBetween } from './utils/rand.js';

describe('random', function () {
it('should not return NaN', function () {
const num = randBetween(0, 100);
assert(!isNaN(num));
const r = randBetween(0, 100);
assert(!isNaN(r));
});
it('should return type Number', function () {
const num = randBetween(0, 100);
assert.equal(typeof num, "number");
it('should return type number', function () {
const r = randBetween(0, 100);
assert.equal(typeof r, "number");
});
it('should be OK with min > max', function () {

let tries = 1000;

while (tries--) {
const r = randBetween(100, 0);
const lessThanMin = r < 0;
const moreThanMax = r > 100;
assert(!lessThanMin, r);
assert(!moreThanMax, r);
}
});
it('should be OK with decimals', function () {
const min = 0.1;
const max = 0.4;
const r = randBetween(min, max);
assert(r >= min, r);
assert(r <= max, r);
});
it('should be able to request decimals with whole bounds', function () {
const min = 1;
const max = 2;
let tries = 100;

let seenDecimal = false;
let r;
while (tries--) {
r = randBetween(min, max, true);
if (parseInt(r) !== r) {
seenDecimal = true;
break;
}
}
assert(seenDecimal, `min:${min}, max:${max}, r:${r}, tries:${99 - tries}`);
});
it('should be OK with negative numbers', function () {
const num = randBetween(-10, 0);
const lessThanMin = num < -10;
const moreThanMax = num > 0;
assert(!lessThanMin, num);
assert(!moreThanMax, num);
const r = randBetween(-10, 0);
const lessThanMin = r < -10;
const moreThanMax = r > 0;
assert(!lessThanMin, r);
assert(!moreThanMax, r);
});
it('should be OK with min 0 and max 1', function () {
const num = randBetween(0, 1);
const lessThanMin = num < 0;
const moreThanMax = num > 1;
assert(!lessThanMin, num);
assert(!moreThanMax, num);
const r = randBetween(0, 1);
const lessThanMin = r < 0;
const moreThanMax = r > 1;
assert(!lessThanMin, r);
assert(!moreThanMax, r);
});
it('should return a number between 0 and 100, 1000 times', function () {

let tries = 1000;

while (tries--) {
const num = randBetween(0, 100);
const lessThanMin = num < 0;
const moreThanMax = num > 100;
assert(!lessThanMin, num);
assert(!moreThanMax, num);
const r = randBetween(0, 100);
const lessThanMin = r < 0;
const moreThanMax = r > 100;
assert(!lessThanMin, r);
assert(!moreThanMax, r);
}
});
it('should be inclusive of min and max (10,20)', function () {
Expand All @@ -42,9 +77,9 @@ describe('random', function () {
let hadMax = false;

while (tries-- && (!hadMin || !hadMax)) {
const num = randBetween(10, 20);
hadMin = hadMin || num === 10;
hadMax = hadMax || num === 20;
const r = randBetween(10, 20);
hadMin = hadMin || r === 10;
hadMax = hadMax || r === 20;
}

assert(hadMin, 'min');
Expand Down
15 changes: 13 additions & 2 deletions src/utils/rand.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const randBetween = (min, max) => {
return Math.floor(Math.random() * (max + 1 - min)) + min;
const randBetween = (min, max, decimal = false) => {

let floor = false;
// If min or max are both integers assume we want whole numbers only
if (!decimal && parseInt(min) === min && parseInt(max) === max) {
max += 1;
floor = true;
}
let rand = (Math.random() * (max - min)) + min;
if (floor) {
rand = Math.floor(rand);
}
return rand;
}

export {
Expand Down

0 comments on commit 542e7db

Please sign in to comment.