Git Product home page Git Product logo

rand-seed's Introduction

NPM Version downloads GitHub Workflow Status Libraries.io dependency status for GitHub repo 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

rand-seed's People

Contributors

michaeldzjap avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.