msrand
v2.0.0
Published
## Table of Contents
Downloads
123
Readme
msrand seeded random number generator
Table of Contents
About
msrand is a seeded random number generator compatible with the Microsoft C run-time-library rand.
The rand function returns a pseudorandom integer in the range 0 to 32,767 (max signed 16 bit integer). As this generates a well-known sequence it is not appropriate for use as a cryptographic function.
It is useful for building games, deterministic randomness to your work, and especially maintaining compatibility with older algorithms such as Jim Horne's freecell dealing algorithm.
Getting Started
You should be able to npm install
this module (or copy-paste it into your
own project).
npm i msrand
Usage
Import msrand using your syntax of choice:
// es6 syntax
import MSRand from msrand;
Then create an instance with an optional seed. If no seed is specified it defaults to 0.
// Create an instance of the random number generator, with a given seed.
const myrng = new MSRand(123);
To get a random integer between 0 and 32,767:
// Return a random integer
myrng.rand();
To get a random integer between 0 and a specified amount:
// Return a random integer from 0-10
myrng.randMax(10);
To get a random decimal between 0 and 1, like you would get from Math.random()
:
// Return a random decimal from 0 to 1
myrng.randDecimal();
If you want a one-off pseudorandom number based on a particular seed you can chain the constructor on one line:
const value = new MSRand("my seed").rand();
Changelog
2.0.0
- This is now a native ES module only. Removed build step & CommonJS support.
- Added
randDecimal
function which as a drop-in pseudorandom replacement forMath.random()
- Added support for passing a string seed. The constructor now support both strings & number seeds.
- Added JSDoc documentation & Typescript support.