Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove seedrandom #65

Merged
merged 9 commits into from
Sep 8, 2024
Merged
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
6 changes: 3 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"root": true,
"extends": ["@fisch0920/eslint-config/node"],
"rules": {
"no-console": "off",
"@typescript-eslint/naming-convention": "off",
"import/consistent-type-specifier-style": "off",
"@typescript-eslint/array-type": "off",
"@typescript-eslint/no-inferrable-types": "off"
"@typescript-eslint/no-inferrable-types": "off",
"unicorn/prefer-code-point": "off",
"unicorn/prefer-math-trunc": "off"
}
}
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
"test:typecheck": "tsc --noEmit",
"test:unit": "vitest run"
},
"dependencies": {
"seedrandom": "^3.0.5"
},
"devDependencies": {
"@fisch0920/eslint-config": "^1.4.0",
"@total-typescript/ts-reset": "^0.6.1",
Expand All @@ -51,6 +48,7 @@
"np": "^10.0.7",
"npm-run-all2": "^6.2.2",
"prettier": "^3.3.3",
"seedrandom": "^3.0.5",
"tsup": "^8.2.4",
"tsx": "^4.19.0",
"typescript": "^5.5.4",
Expand All @@ -65,6 +63,7 @@
"prng",
"stats",
"d3-random",
"probability",
"seedrandom",
"distribution",
"pseudorandom",
Expand Down
7 changes: 3 additions & 4 deletions pnpm-lock.yaml

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

69 changes: 15 additions & 54 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,18 @@ Welcome to the most **random** module on npm! 😜

## Highlights <!-- omit in toc -->

- Simple TS API
- Supports all modern JS/TS runtimes
- Seedable based on entropy or user input
- Plugin support for different pseudo random number generators (PRNGs)
- Simple TS API with zero dependencies
- **Seedable**
- Plugin support for different pseudo random number generators
- Includes many common distributions
- uniform, normal, poisson, bernoulli, etc
- Validates all user input
- Integrates with [seedrandom](https://github.com/davidbau/seedrandom)
- Replacement for `seedrandom` which hasn't been updated in over 5 years
- Supports all modern JS/TS runtimes

## Install <!-- omit in toc -->

```bash
npm install --save random
# or
yarn add random
# or
pnpm add random
npm install random
```

## Usage <!-- omit in toc -->
Expand Down Expand Up @@ -88,29 +83,22 @@ poisson() // 4
poisson() // 1
```

Note that returning a thunk here is more efficient when generating multiple
samples from the same distribution.
Note that returning a thunk here is more efficient when generating multiple samples from the same distribution.

You can change the underlying PRNG or its seed as follows:

```ts
import seedrandom from 'seedrandom'
// change the underlying pseudo random number generator seed.
// by default, Math.random is used as the underlying PRNG, but it is not seedable,
// so if a seed is given, we use an ARC4 PRNG.
random.use('my-seed')

// change the underlying pseudo random number generator
// by default, Math.random is used as the underlying PRNG
random.use(seedrandom('foobar'))

// create a new independent random number generator (uses seedrandom under the hood)
// create a new independent random number generator (uses ARC4 under the hood)
const rng = random.clone('my-new-seed')

// create a second independent random number generator and use a seeded PRNG
// create a second independent random number generator using a custom PRNG
import seedrandom from 'seedrandom'
const rng2 = random.clone(seedrandom('kittyfoo'))

// replace Math.random with rng.uniform
rng.patch()

// restore original Math.random
rng.unpatch()
```

You can also instantiate a fresh instance of `Random`:
Expand All @@ -136,8 +124,6 @@ const rng3 = new Random(seedrandom('my-seed-string'))
- [rng](#rng)
- [clone](#clone)
- [use](#use)
- [patch](#patch)
- [unpatch](#unpatch)
- [next](#next)
- [float](#float)
- [int](#int)
Expand Down Expand Up @@ -218,22 +204,6 @@ random.use(Math.random)

---

#### [patch](https://github.com/transitive-bullshit/random/blob/e11a840a1cfe0f5bd9c43640f9645a0b28f61406/src/random.js#L94-L101)

Patches `Math.random` with this Random instance's PRNG.

Type: `function ()`

---

#### [unpatch](https://github.com/transitive-bullshit/random/blob/e11a840a1cfe0f5bd9c43640f9645a0b28f61406/src/random.js#L106-L111)

Restores a previously patched `Math.random` to its original value.

Type: `function ()`

---

#### [next](https://github.com/transitive-bullshit/random/blob/e11a840a1cfe0f5bd9c43640f9645a0b28f61406/src/random.js#L124-L126)

Convenience wrapper around `this.rng.next()`
Expand Down Expand Up @@ -485,16 +455,7 @@ Type: `function (alpha): function`
- Generators

- [x] pluggable prng
- [ ] port more prng from boost
- [ ] custom entropy

- Misc
- [x] browser support via rollup
- [x] basic docs
- [x] basic tests
- [x] test suite
- [x] initial release!
- [x] typescript support
- [ ] port more prng from boost / seedrandom

## Related <!-- omit in toc -->

Expand Down
200 changes: 100 additions & 100 deletions src/__snapshots__/seed.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -107,105 +107,105 @@ exports[`random.clone with seedrandom rng is consistent 1`] = `

exports[`random.clone with string seed is consistent 1`] = `
[
0.4599885692070501,
1.498943037863171,
-0.45711036116856824,
1.198323491226302,
0.31744349762199836,
0.2685493132319243,
-0.1045591660009993,
0.49085805160430834,
-0.8654625222187131,
1.0328218787419141,
0.08718695044417465,
-2.15550607808736,
-2.1319639855429267,
0.0338703357113602,
-1.48263309903668,
0.4704950584394675,
0.7476730169358645,
1.351653203830535,
-0.9828311064313547,
2.1405398829122455,
0.40888838714250364,
1.423884889193753,
-0.4202365483836478,
-0.6292491126479662,
-0.06490555468136183,
-2.407099109475531,
0.32361017371377715,
-0.4438871374219309,
-0.2346580940412258,
1.0281352299125754,
-1.6882299086494705,
-1.473245383801771,
0.8357555103173332,
0.7135368445932185,
-0.7578513303659669,
-0.3387116629465658,
-0.4153256830832315,
-0.6688468771750677,
0.4167651567060368,
0.28869308064318294,
-0.5815805569463813,
-1.3495568924563546,
-0.5380190897913749,
-0.9973987475284024,
0.5901468209689925,
0.05511991679885503,
-0.5592048510240638,
0.647797683123219,
0.9328861463637381,
-0.9107368893790824,
-1.5202624973614696,
1.9619960339057412,
-0.2564540016287483,
1.8386441386224903,
-0.5597862507291175,
-0.5163295347733488,
0.20253589017784882,
-1.1290363774605272,
-1.3852662932430548,
0.4105908858480398,
-0.8670786023272543,
-0.37114554286023166,
-1.429674546123385,
0.1766914665610556,
-0.7577884069438263,
2.015582353783945,
-1.3383970039777633,
-2.294167384222547,
1.8627818074176121,
1.034364085597537,
0.12766688422696987,
-0.09821117867725376,
-0.49714043547906833,
0.8428332981015355,
0.8132636354024767,
-0.49862783315847153,
-0.674525692894474,
-0.16560513623146625,
-0.6912392012751473,
1.0125818546132972,
1.5559982434188717,
-0.6998095951192632,
1.2507104808040281,
0.630302824033449,
-0.11980026990636526,
0.1604586588015088,
0.14859821202202345,
-1.1874345546175447,
-0.0006786940627859577,
0.5206751108384351,
-0.11254950785067822,
1.0632337013426334,
-0.13003669756609917,
-1.32542353979745,
-0.5616800941504286,
1.2987971837982453,
0.04547221580867748,
-0.27036136048909387,
0.20371364246834672,
1.5437165642697632,
1.0605080478180011,
0.3824102148617453,
-0.7366504345478391,
-0.7946475045739224,
-1.2247324314330592,
0.6716850060654046,
-1.009299614178395,
0.053268550981502426,
0.9740577508148593,
-0.6752585320928494,
0.7446218073301792,
-2.129054359211667,
-1.8318170942537888,
0.12094888734754691,
-0.10445788111868438,
-0.08857052877829866,
-1.2097296776821065,
-0.117177897638088,
0.589262412952502,
-0.31461503408417685,
1.5492120933673714,
-0.30159579028234024,
0.10169022209103494,
-1.1878904212756178,
-0.4045334642187247,
-1.7727229412321357,
-2.1370757665469235,
2.552039624962811,
0.116684628880318,
-0.0738586836791935,
-0.7462649962349035,
-0.7308953270140024,
-1.397496377452742,
1.080022871482886,
-0.5550935933831801,
-2.166285498075109,
-1.4202974080672033,
0.13941215473562024,
1.9511413086889442,
-0.24760951962395597,
-0.7668772257957995,
1.0080903850107452,
-0.46433681035376906,
0.9140407264550278,
-0.7321078574285467,
2.2728031290005806,
0.7831495249798555,
-1.1747809631263495,
0.5033686835114866,
-0.5853630434209829,
0.8048849983338022,
-0.6120558584970164,
-0.511942670460981,
0.6621138875933301,
-0.2962382844043797,
0.17405575536871304,
0.29531818471271,
-0.14469245026793423,
0.7323251168391561,
1.4823525216244526,
-0.5573550292228768,
0.4219991769658255,
-0.07575077050239393,
-0.9185841160043985,
0.9385302429828497,
0.2425634286777713,
0.8670907746244069,
-0.27307985429422366,
0.9879088784100294,
0.5647239501014518,
-1.2854231936567626,
0.3697503934710063,
0.03015265871219791,
1.4794228859998708,
-1.2900824469544805,
1.2416773731568156,
-0.08836086943630893,
0.44847317559380423,
0.35891377843696987,
-1.3734860166194451,
-0.22825428479145904,
0.24768202437532366,
-0.03862140436191103,
1.3852892668021894,
0.07026553111435546,
-0.7807561264439524,
0.2570017824237705,
-0.8189485607920781,
-1.274011647761965,
-0.7026654258228537,
-0.5601442639711995,
-0.48577032219059707,
0.8647210761817536,
-0.7017979621827999,
1.3803710519208559,
0.47852874862935263,
-0.7706566252379807,
-0.42212246077683613,
0.4452340452580614,
0.5214098824451193,
]
`;
2 changes: 1 addition & 1 deletion src/distributions/bates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Random } from '../random'
import type { Random } from '../random'
import { numberValidator } from '../validation'

export function bates(random: Random, n = 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/distributions/bernoulli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Random } from '../random'
import type { Random } from '../random'
import { numberValidator } from '../validation'

export function bernoulli(random: Random, p = 0.5) {
Expand Down
Loading