Skip to content
This repository has been archived by the owner on Dec 22, 2022. It is now read-only.

Commit

Permalink
added option for custom prng to random
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisweb committed Aug 31, 2020
1 parent fd1d678 commit e2ae560
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 22 deletions.
4 changes: 2 additions & 2 deletions dist/@types/chrisweb-utilities/utilities/random.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
* @param min
* @param max
*/
declare const randomInteger: (min?: number, max?: number) => number;
declare const choice: (sequence?: unknown[]) => unknown;
declare const randomInteger: (min?: number, max?: number, prng?: typeof Math) => number;
declare const choice: (sequence?: unknown[], prng?: typeof Math) => unknown;
export { randomInteger, choice };
20 changes: 16 additions & 4 deletions dist/index.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.esm.js.map

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chrisweb-utilities",
"version": "1.3.0",
"version": "1.3.1",
"description": "chrisweb utilities",
"types": "dist/@types/chrisweb-utilities/index.d.ts",
"scripts": {
Expand Down Expand Up @@ -35,7 +35,7 @@
"devDependencies": {
"@types/jest": "26.0.10",
"jest": "26.4.2",
"rollup": "2.26.6",
"rollup": "2.26.8",
"rollup-plugin-typescript2": "0.27.2",
"ts-jest": "26.3.0",
"eslint": "7.7.0",
Expand Down
18 changes: 14 additions & 4 deletions src/utilities/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
* @param min
* @param max
*/
const randomInteger = (min = 0, max = Infinity): number => {
return Math.floor(Math.random() * (max - min + 1) + min);
const randomInteger = (min = 0, max = Infinity, prng?: typeof Math): number => {
// support for custom PRNGs, like https://www.npmjs.com/package/seedrandom
if (prng) {
prng.floor(prng.random() * (max - min + 1) + min);
} else {
return Math.floor(Math.random() * (max - min + 1) + min);
}
}

const choice = (sequence: unknown[] = []): unknown => {
return sequence[Math.floor(Math.random() * sequence.length)];
const choice = (sequence: unknown[] = [], prng?: typeof Math): unknown => {
// support for custom PRNGs, like https://www.npmjs.com/package/seedrandom
if (prng) {
return sequence[prng.floor(prng.random() * sequence.length)];
} else {
return sequence[Math.floor(Math.random() * sequence.length)];
}
}

export { randomInteger, choice }

0 comments on commit e2ae560

Please sign in to comment.