Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/algorithms/lib.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Get a random number between minimum and maximum number
// Get a random integer between minimum and maximum numbers (inclusive)
export function randomNum(min, max) {
return Math.floor(Math.random() * (max - min) + min);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
16 changes: 16 additions & 0 deletions src/algorithms/lib.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { randomNum } from './lib';

test('randomNum returns values within range and includes max', () => {
const min = 1;
const max = 2;
let sawMax = false;
for (let i = 0; i < 1000; i++) {
const value = randomNum(min, max);
expect(value).toBeGreaterThanOrEqual(min);
expect(value).toBeLessThanOrEqual(max);
if (value === max) {
sawMax = true;
}
}
expect(sawMax).toBe(true);
});