Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Seeded random number generators for TypeScript.

License

NotificationsYou must be signed in to change notification settings

jurerotar/ts-seedrandom

Repository files navigation

Seeded random number generators for TypeScript.

Installation

npm install ts-seedrandom

Usage

Each generator includes the following methods:

  • quick - Default method used. Provides 32 bits of randomness in a float. Can either be called by calling generator instance directly (ex.generator()) or by name (ex.generator.quick()).
  • double - Provides 56 bits of randomness.
  • int32 - Providers a 32 bit (signed) integer.
  • state - Provides internal generator state. Used for saving and restoring states.
import{prngAlea}from'ts-seedrandom';constaleaGenerator=prngAlea('seed');constfirstValue=aleaGenerator();constsecondValue=aleaGenerator();

You also have the option of saving and restoring state of your generator.

import{prngAlea}from'ts-seedrandom';constaleaGenerator=prngAlea('seed');constfirstValue=aleaGenerator();// Return internal generator state, which you can use in other generator instancesconststate=aleaGenerator.state();// This generator starts from the same state as first generator, but runs independentlyconstsecondAleaGenerator=prngAlea('seed',state);

Available Algorithms

The following PRNG algorithms are available:

  1. prngAlea: Alea algorithm
  2. prngArc4: ARC4 algorithm
  3. prngTychei: Tyche-i algorithm
  4. prngXor128: XorShift128 algorithm
  5. prngXor4096: XorShift4096 algorithm
  6. prngXorshift7: XorShift7 algorithm
  7. prngXorwow: Xorwow algorithm
  8. prngMulberry32: Mulberry 32 algorithm
  9. prngXoshiro128Plus: Xoshiro128+ algorithm
  10. prngXoshiro128PlusPlus: Xoshiro128++ algorithm
  11. prngSplitMix64: SplitMix64 algorithm
  12. prngSplitMix32: SplitMix32 algorithm
  13. prngSfc32: SFC32 algorithm
  14. prngJsf32: JSF32 algorithm
  15. prngXoroshiro128ss: Xoshiro128** algorithm
  16. prngXoroshiro128+: Xoroshiro128plus algorithm
  17. prngParkMiller: Lehrer (Park-Miller) algorithm

You can import and use any of these algorithms in the same way as demonstrated in the usage examples above.

Algorithm comparison (fastest → slowest)

NameState SizeTime for 1M iters (ms)Speed (Mops/s)Per-iter (ns)× slowerSlower vs fastest
xor128128 bits10.2697.4210.261.00×0.0%
xor40964096 bits10.3396.8310.331.01×0.6%
xorwow192 bits11.8084.7111.801.15×15.0%
xorshift7256 bits12.3980.7412.391.21×20.7%
splitMix3232 bits13.4574.3513.451.31×31.0%
mulberry3232 bits13.6273.4413.621.33×32.6%
tychei128 bits15.7563.5115.751.53×53.4%
xoshiro128+128 bits16.0862.1916.081.57×56.7%
xoshiro128++128 bits16.9958.8616.991.66×65.5%
parkMiller31 bits17.4857.2017.481.70×70.3%
alea~96 bits19.0152.6019.011.85×85.2%
sfc32128 bits26.0138.4526.012.53×153.4%
jsf32128 bits37.4126.7337.413.64×264.4%
arc42048 bits86.6011.5586.608.44×743.7%
pcg32128 bits156.636.38156.6315.26×1425.8%
xoroshiro128plus128 bits261.903.82261.9025.51×2451.4%
xoroshiro128ss128 bits373.662.68373.6636.40×3540.2%
splitmix6464 bits632.141.58632.1461.58×6058.2%

Notes on the numbers

  • What I computed:

    • Mops/s (million iters/sec) = 1000 / (time_ms)
    • per-iteration (ns) ≈ time_ms
    • Slower vs fastest (%) = (1 - (current_speed / fastest_speed)) * 100.
  • Test details / machine:Lenovo Legion 5 Pro 16ACH6H (Ryzen 7 5800H — 8 cores / 16 threads, base ≈ 3.2 GHz, turbo ≈ 4.4 GHz, DDR4-3200 memory); Node.js v24.10.0.

  • Why numbers vary: JIT warm-up, Node version, single vs multi-thread scheduling, background load, and micro-optimizations in each PRNG implementation all affect timings. Use these as a relative ranking on this machine, not an absolute cross-platform benchmark.

  • You can replicate this exact table by runningnpm run compare:performance


[8]ページ先頭

©2009-2025 Movatter.jp