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 JavaScript, ported to 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 (simple, fast)
  10. prngXoshiro128PlusPlus: Xoshiro128++ algorithm (higher statistical quality)
  11. prngSplitMix64: SplitMix64 algorithm
  12. prngPcg32: PCG32 algorithm

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

Algorithm comparison

NameState SizeSpeed 🏎️Quality 📊PeriodNotes
prngAlea~96 bits🟢 Fast🔸 OK~2³²Simple and widely used in JS
prngArc42048 bits🟡 Medium🔸 OK~2¹⁷⁰⁰Legacy cipher, good entropy, slowish
prngTychei128 bits🟢 Fast🟡 Medium~2⁶⁴Inspired by Marsaglia, fast and small
prngXor128128 bits🟢 Fast🔸 OK~2¹²⁸−1Basic Xorshift, outdated
prngXor40964096 bits🔴 Slow🟡 Medium~2⁴⁰⁹⁶−1Very large state
prngXorshift7224 bits🟡 Medium🟡 Medium~2²²⁴Passes more tests than basic Xorshift
prngXorwow160 bits🟢 Fast🟡 Medium~2¹⁹²Used in older CUDA RNGs
prngMulberry3232 bits🟢 Very fast🔸 OK~2³²Very small, ultra fast
prngXoshiro128Plus128 bits🟢 Fast🟡 Good~2¹²⁸−1Simple, good for games
prngXoshiro128PlusPlus128 bits🟢 Fast🟢 Better~2¹²⁸−1Improved output function
prngSplitMix6464 bits🟢 Very fast🟢 High~2⁶⁴Great for seeding other PRNGs
prngPcg3264 bits🟢 Fast🟢 High~2⁶⁴Compact, modern, good for simulation use

Legend

  • Speed:

    • 🟢 Fast: Suitable for games, animation, UI
    • 🟡 Medium: Acceptable overhead
    • 🔴 Slow: Use only when large state is essential
  • Quality:

    • 🔸 OK: May fail strict tests (e.g., PractRand)
    • 🟡 Medium: Passes basic uniformity/randomness
    • 🟢 High: Strong statistical quality

About

Seeded random number generators for JavaScript, ported to TypeScript.

Topics

Resources

License

Stars

Watchers

Forks


[8]ページ先頭

©2009-2025 Movatter.jp