Skip to content

Commit 6a8809c

Browse files
Merge pull request #65 from transitive-bullshit/feature/arc4-remove-seed-random
2 parents e22071d + 7cd4226 commit 6a8809c

33 files changed

+438
-390
lines changed

.eslintrc.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"root": true,
33
"extends": ["@fisch0920/eslint-config/node"],
44
"rules": {
5-
"no-console": "off",
65
"@typescript-eslint/naming-convention": "off",
7-
"import/consistent-type-specifier-style": "off",
86
"@typescript-eslint/array-type": "off",
9-
"@typescript-eslint/no-inferrable-types": "off"
7+
"@typescript-eslint/no-inferrable-types": "off",
8+
"unicorn/prefer-code-point": "off",
9+
"unicorn/prefer-math-trunc": "off"
1010
}
1111
}

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
"test:typecheck": "tsc --noEmit",
3737
"test:unit": "vitest run"
3838
},
39-
"dependencies": {
40-
"seedrandom": "^3.0.5"
41-
},
4239
"devDependencies": {
4340
"@fisch0920/eslint-config": "^1.4.0",
4441
"@total-typescript/ts-reset": "^0.6.1",
@@ -51,6 +48,7 @@
5148
"np": "^10.0.7",
5249
"npm-run-all2": "^6.2.2",
5350
"prettier": "^3.3.3",
51+
"seedrandom": "^3.0.5",
5452
"tsup": "^8.2.4",
5553
"tsx": "^4.19.0",
5654
"typescript": "^5.5.4",
@@ -65,6 +63,7 @@
6563
"prng",
6664
"stats",
6765
"d3-random",
66+
"probability",
6867
"seedrandom",
6968
"distribution",
7069
"pseudorandom",

pnpm-lock.yaml

+3-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.md

+15-54
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,18 @@ Welcome to the most **random** module on npm! 😜
88

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

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

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

2221
```bash
23-
npm install --save random
24-
# or
25-
yarn add random
26-
# or
27-
pnpm add random
22+
npm install random
2823
```
2924

3025
## Usage <!-- omit in toc -->
@@ -88,29 +83,22 @@ poisson() // 4
8883
poisson() // 1
8984
```
9085

91-
Note that returning a thunk here is more efficient when generating multiple
92-
samples from the same distribution.
86+
Note that returning a thunk here is more efficient when generating multiple samples from the same distribution.
9387

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

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

99-
// change the underlying pseudo random number generator
100-
// by default, Math.random is used as the underlying PRNG
101-
random.use(seedrandom('foobar'))
102-
103-
// create a new independent random number generator (uses seedrandom under the hood)
96+
// create a new independent random number generator (uses ARC4 under the hood)
10497
const rng = random.clone('my-new-seed')
10598

106-
// create a second independent random number generator and use a seeded PRNG
99+
// create a second independent random number generator using a custom PRNG
100+
import seedrandom from 'seedrandom'
107101
const rng2 = random.clone(seedrandom('kittyfoo'))
108-
109-
// replace Math.random with rng.uniform
110-
rng.patch()
111-
112-
// restore original Math.random
113-
rng.unpatch()
114102
```
115103

116104
You can also instantiate a fresh instance of `Random`:
@@ -136,8 +124,6 @@ const rng3 = new Random(seedrandom('my-seed-string'))
136124
- [rng](#rng)
137125
- [clone](#clone)
138126
- [use](#use)
139-
- [patch](#patch)
140-
- [unpatch](#unpatch)
141127
- [next](#next)
142128
- [float](#float)
143129
- [int](#int)
@@ -218,22 +204,6 @@ random.use(Math.random)
218204

219205
---
220206

221-
#### [patch](https://github.com/transitive-bullshit/random/blob/e11a840a1cfe0f5bd9c43640f9645a0b28f61406/src/random.js#L94-L101)
222-
223-
Patches `Math.random` with this Random instance's PRNG.
224-
225-
Type: `function ()`
226-
227-
---
228-
229-
#### [unpatch](https://github.com/transitive-bullshit/random/blob/e11a840a1cfe0f5bd9c43640f9645a0b28f61406/src/random.js#L106-L111)
230-
231-
Restores a previously patched `Math.random` to its original value.
232-
233-
Type: `function ()`
234-
235-
---
236-
237207
#### [next](https://github.com/transitive-bullshit/random/blob/e11a840a1cfe0f5bd9c43640f9645a0b28f61406/src/random.js#L124-L126)
238208

239209
Convenience wrapper around `this.rng.next()`
@@ -485,16 +455,7 @@ Type: `function (alpha): function`
485455
- Generators
486456

487457
- [x] pluggable prng
488-
- [ ] port more prng from boost
489-
- [ ] custom entropy
490-
491-
- Misc
492-
- [x] browser support via rollup
493-
- [x] basic docs
494-
- [x] basic tests
495-
- [x] test suite
496-
- [x] initial release!
497-
- [x] typescript support
458+
- [ ] port more prng from boost / seedrandom
498459

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

src/__snapshots__/seed.test.ts.snap

+100-100
Original file line numberDiff line numberDiff line change
@@ -107,105 +107,105 @@ exports[`random.clone with seedrandom rng is consistent 1`] = `
107107

108108
exports[`random.clone with string seed is consistent 1`] = `
109109
[
110-
0.4599885692070501,
111-
1.498943037863171,
112-
-0.45711036116856824,
113-
1.198323491226302,
114-
0.31744349762199836,
115-
0.2685493132319243,
116-
-0.1045591660009993,
117-
0.49085805160430834,
118-
-0.8654625222187131,
119-
1.0328218787419141,
120-
0.08718695044417465,
121-
-2.15550607808736,
122-
-2.1319639855429267,
123-
0.0338703357113602,
124-
-1.48263309903668,
125-
0.4704950584394675,
126-
0.7476730169358645,
127-
1.351653203830535,
128-
-0.9828311064313547,
129-
2.1405398829122455,
130-
0.40888838714250364,
131-
1.423884889193753,
132-
-0.4202365483836478,
133-
-0.6292491126479662,
134-
-0.06490555468136183,
135-
-2.407099109475531,
136-
0.32361017371377715,
137-
-0.4438871374219309,
138-
-0.2346580940412258,
139-
1.0281352299125754,
140-
-1.6882299086494705,
141-
-1.473245383801771,
142-
0.8357555103173332,
143-
0.7135368445932185,
144-
-0.7578513303659669,
145-
-0.3387116629465658,
146-
-0.4153256830832315,
147-
-0.6688468771750677,
148-
0.4167651567060368,
149-
0.28869308064318294,
150-
-0.5815805569463813,
151-
-1.3495568924563546,
152-
-0.5380190897913749,
153-
-0.9973987475284024,
154-
0.5901468209689925,
155-
0.05511991679885503,
156-
-0.5592048510240638,
157-
0.647797683123219,
158-
0.9328861463637381,
159-
-0.9107368893790824,
160-
-1.5202624973614696,
161-
1.9619960339057412,
162-
-0.2564540016287483,
163-
1.8386441386224903,
164-
-0.5597862507291175,
165-
-0.5163295347733488,
166-
0.20253589017784882,
167-
-1.1290363774605272,
168-
-1.3852662932430548,
169-
0.4105908858480398,
170-
-0.8670786023272543,
171-
-0.37114554286023166,
172-
-1.429674546123385,
173-
0.1766914665610556,
174-
-0.7577884069438263,
175-
2.015582353783945,
176-
-1.3383970039777633,
177-
-2.294167384222547,
178-
1.8627818074176121,
179-
1.034364085597537,
180-
0.12766688422696987,
181-
-0.09821117867725376,
182-
-0.49714043547906833,
183-
0.8428332981015355,
184-
0.8132636354024767,
185-
-0.49862783315847153,
186-
-0.674525692894474,
187-
-0.16560513623146625,
188-
-0.6912392012751473,
189-
1.0125818546132972,
190-
1.5559982434188717,
191-
-0.6998095951192632,
192-
1.2507104808040281,
193-
0.630302824033449,
194-
-0.11980026990636526,
195-
0.1604586588015088,
196-
0.14859821202202345,
197-
-1.1874345546175447,
198-
-0.0006786940627859577,
199-
0.5206751108384351,
200-
-0.11254950785067822,
201-
1.0632337013426334,
202-
-0.13003669756609917,
203-
-1.32542353979745,
204-
-0.5616800941504286,
205-
1.2987971837982453,
206-
0.04547221580867748,
207-
-0.27036136048909387,
208-
0.20371364246834672,
209-
1.5437165642697632,
110+
1.0605080478180011,
111+
0.3824102148617453,
112+
-0.7366504345478391,
113+
-0.7946475045739224,
114+
-1.2247324314330592,
115+
0.6716850060654046,
116+
-1.009299614178395,
117+
0.053268550981502426,
118+
0.9740577508148593,
119+
-0.6752585320928494,
120+
0.7446218073301792,
121+
-2.129054359211667,
122+
-1.8318170942537888,
123+
0.12094888734754691,
124+
-0.10445788111868438,
125+
-0.08857052877829866,
126+
-1.2097296776821065,
127+
-0.117177897638088,
128+
0.589262412952502,
129+
-0.31461503408417685,
130+
1.5492120933673714,
131+
-0.30159579028234024,
132+
0.10169022209103494,
133+
-1.1878904212756178,
134+
-0.4045334642187247,
135+
-1.7727229412321357,
136+
-2.1370757665469235,
137+
2.552039624962811,
138+
0.116684628880318,
139+
-0.0738586836791935,
140+
-0.7462649962349035,
141+
-0.7308953270140024,
142+
-1.397496377452742,
143+
1.080022871482886,
144+
-0.5550935933831801,
145+
-2.166285498075109,
146+
-1.4202974080672033,
147+
0.13941215473562024,
148+
1.9511413086889442,
149+
-0.24760951962395597,
150+
-0.7668772257957995,
151+
1.0080903850107452,
152+
-0.46433681035376906,
153+
0.9140407264550278,
154+
-0.7321078574285467,
155+
2.2728031290005806,
156+
0.7831495249798555,
157+
-1.1747809631263495,
158+
0.5033686835114866,
159+
-0.5853630434209829,
160+
0.8048849983338022,
161+
-0.6120558584970164,
162+
-0.511942670460981,
163+
0.6621138875933301,
164+
-0.2962382844043797,
165+
0.17405575536871304,
166+
0.29531818471271,
167+
-0.14469245026793423,
168+
0.7323251168391561,
169+
1.4823525216244526,
170+
-0.5573550292228768,
171+
0.4219991769658255,
172+
-0.07575077050239393,
173+
-0.9185841160043985,
174+
0.9385302429828497,
175+
0.2425634286777713,
176+
0.8670907746244069,
177+
-0.27307985429422366,
178+
0.9879088784100294,
179+
0.5647239501014518,
180+
-1.2854231936567626,
181+
0.3697503934710063,
182+
0.03015265871219791,
183+
1.4794228859998708,
184+
-1.2900824469544805,
185+
1.2416773731568156,
186+
-0.08836086943630893,
187+
0.44847317559380423,
188+
0.35891377843696987,
189+
-1.3734860166194451,
190+
-0.22825428479145904,
191+
0.24768202437532366,
192+
-0.03862140436191103,
193+
1.3852892668021894,
194+
0.07026553111435546,
195+
-0.7807561264439524,
196+
0.2570017824237705,
197+
-0.8189485607920781,
198+
-1.274011647761965,
199+
-0.7026654258228537,
200+
-0.5601442639711995,
201+
-0.48577032219059707,
202+
0.8647210761817536,
203+
-0.7017979621827999,
204+
1.3803710519208559,
205+
0.47852874862935263,
206+
-0.7706566252379807,
207+
-0.42212246077683613,
208+
0.4452340452580614,
209+
0.5214098824451193,
210210
]
211211
`;

src/distributions/bates.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Random } from '../random'
1+
import type { Random } from '../random'
22
import { numberValidator } from '../validation'
33

44
export function bates(random: Random, n = 1) {

src/distributions/bernoulli.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Random } from '../random'
1+
import type { Random } from '../random'
22
import { numberValidator } from '../validation'
33

44
export function bernoulli(random: Random, p = 0.5) {

0 commit comments

Comments
 (0)