Socket
Socket
Sign inDemoInstall

rand-seed

Package Overview
Dependencies
0
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    rand-seed

A small library for generating random numbers


Version published
Maintainers
1
Install size
11.5 kB
Created

Readme

Source

NPM Version downloads Build Status dependencies tested with jest codecov Quality Gate Status License

rand-seed

A small seedable random number generator library written in TypeScript

The default Math.random() function doesn't allow for setting a seed. This library offers a number of different semi-random number generators which may be initialised with an arbitrary seed in string format, hence, making it possible to produce sequences of semi-random numbers that are always the same for a given seed. The implemented algorithms are detailed here (note that I am not the author of that post, I merely used it for the implementation of this library).

Installation

This package is available through npm:

npm install --save rand-seed

Usage

Either import directly

<script src="path-to-rand-seed/rand-seed.js"></script>

or import in your own scripts using

import Rand, {PRNG} from 'rand-seed';

Then simply create a new instance with an (optional) seed:

const rand = new Rand('1234');

rand.next(); // Generate a new random number

If no seed is specified the call to rand.next() will simply be forwarded to Math.random(). In case of a supplied seed, the sfc32 algorithm will be used by default. Currently, three different algorithms are provided: sfc32, mulberry32 and xoshiro128**. If you would like to use a different algorithm, create a new instance like so:

// Create a new random number generator using the xoshiro128** algorithm
const rand = new Rand('1234', PRNG.xoshiro128ss);

Example

A simple example is included. This may be run with node: node example/index.js

Keywords

FAQs

Last updated on 14 Sep 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc