-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
14c97c9
commit 4a190c5
Showing
3 changed files
with
21 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { getRandomValue } from "./getRandomValue"; | ||
|
||
describe("Get random value", () => { | ||
it("should return random int value from 0-2 provided range 3", () => { | ||
const randomValue = getRandomValue({ range: 3 }); | ||
expect([0, 1, 2].includes(randomValue)).toBe(true); | ||
}); | ||
|
||
it("should return random int value from 0-4 provided range 5", () => { | ||
const randomValue = getRandomValue({ range: 5 }); | ||
expect([0, 1, 2, 3, 4].includes(randomValue)).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import crypto from "crypto"; | ||
|
||
export const getRandomValue = ({ range }: { range: number }) => { | ||
const array = new Uint32Array(1); // creates array with length only 1 | ||
return crypto.getRandomValues(array)[0] % range; //thus accessing first position in the array | ||
}; |