All Projects → DomenicoDeFelice → jsrand

DomenicoDeFelice / jsrand

Licence: MIT license
A seeded pseudo-random number generator for JavaScript.

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to jsrand

prvhash
PRVHASH - Pseudo-Random-Value Hash. Hash functions, PRNG with unlimited period, randomness extractor. (Codename Gradilac/Градилак)
Stars: ✭ 194 (+921.05%)
Mutual labels:  random, prng, pseudorandom, pseudo-random
komihash
Very fast, high-quality hash function (non-cryptographic, C) + PRNG
Stars: ✭ 68 (+257.89%)
Mutual labels:  prng, pseudorandom, pseudo-random
SharpLoader
🔮 [C#] Source code randomizer and compiler
Stars: ✭ 36 (+89.47%)
Mutual labels:  random, seed
msp430-rng
Random (SLAA338) and pseudorandom (LCG) number generation.
Stars: ✭ 19 (+0%)
Mutual labels:  random, pseudorandom
randoma
User-friendly pseudorandom number generator (PRNG)
Stars: ✭ 103 (+442.11%)
Mutual labels:  prng, pseudorandom
RNG
A simple state-of-the-art C++ random number generator
Stars: ✭ 19 (+0%)
Mutual labels:  random, prng
random
The most random module on npm
Stars: ✭ 106 (+457.89%)
Mutual labels:  random, prng
secrets.clj
A library designed to generate cryptographically strong random numbers.
Stars: ✭ 64 (+236.84%)
Mutual labels:  random, prng
Gofakeit
Random fake data generator written in go
Stars: ✭ 2,193 (+11442.11%)
Mutual labels:  random, seed
Rocket
ROCKET: Exceptionally fast and accurate time series classification using random convolutional kernels
Stars: ✭ 163 (+757.89%)
Mutual labels:  random
Mathnet Numerics
Math.NET Numerics
Stars: ✭ 2,688 (+14047.37%)
Mutual labels:  random
Randomatic
Easily generate random strings like passwords, with simple options for specifying a length and for using patterns of numeric, alpha-numeric, alphabetical, special or custom characters. (the original "generate-password")
Stars: ✭ 149 (+684.21%)
Mutual labels:  random
Nyaya
Random Data Generation and/or Property Testing in Scala & Scala.JS.
Stars: ✭ 165 (+768.42%)
Mutual labels:  random
Cat Names
🐈 Get popular cat names
Stars: ✭ 226 (+1089.47%)
Mutual labels:  random
Randomdata
Random data generator
Stars: ✭ 157 (+726.32%)
Mutual labels:  random
rainbow-bash-prompt
Make your bash prompt dynamically and randomly rainbow
Stars: ✭ 49 (+157.89%)
Mutual labels:  random
Jnanoid
A unique string ID generator for Java.
Stars: ✭ 147 (+673.68%)
Mutual labels:  random
Pandemonium
Typical random-related functions for JavaScript and TypeScript.
Stars: ✭ 144 (+657.89%)
Mutual labels:  random
aes-stream
A fast AES-PRF based secure random-number generator
Stars: ✭ 15 (-21.05%)
Mutual labels:  random
Python Gift Exchange
pyge: Holiday Gift Exchange Picker
Stars: ✭ 252 (+1226.32%)
Mutual labels:  random

jsrand a.k.a. seeded-rand

Build Status Build Size NPM Downloads NPM

A seeded pseudo-random number generator for JavaScript.

It can be used as either a plain script or as a Node.js module.

Numbers are generated using a one-seeded version of the multiply-with-carry method by George Marsaglia. While this method is okay for most applications, it is not cryptographically strong.

jsrand supports saving and restoring the generator state and common operations on arrays: choice (pick a random element), choices (pick elements at random), sample (pick elements at random without repetition) and shuffle.

See changelog here.

Table of contents

Install

NPM

$ npm install seeded-rand

Plain script

Just download dist/jsrand.min.js and (optionally) dist/jsrand.min.js.map and include it in your app.

Usage

Plain script NPM
<script src="jsrand.min.js"></script>

This will define a global Srand object. If the name Srand is already taken, see noConflict.

const Srand = require('seeded-rand');

or

import Srand from 'seeded-rand';

All methods can be used either statically:

Srand.seed(10); // 10
Srand.random(); // 0.4569510892033577

or instantiating a new generator:

const rnd = new Srand(10);
rnd.random(); // 0.4569510892033577

const othr = new Srand(rnd.seed());
othr.random(); // 0.4569510892033577

Examples

const rnd = new Srand(); // Initiate with random seed

rnd.seed(); // 1836504610 Read the seed
rnd.randomize(); // 3409024789 Random seed is set and returned
rnd.seed(1836504610); // 1836504610 Set a seed

rnd.inRange(0, 10); // 6.866552880965173
rnd.intInRange(0, 10); // 1

rnd.choice([1, 2, 3]); // 3
rnd.choices([1, 2, 3], 3); // [3, 3, 1] possible repetitions
rnd.choices([1, 2, 3], 3); // [2, 2, 3] possible repetitions

rnd.sample([1, 2, 3], 2); // [1, 2] no repetitions
rnd.sample([1, 2, 3], 2); // [1, 2] no repetitions

const state = rnd.getState();
rnd.intInRange(1, 50); // 39
rnd.intInRange(1, 50); // 24
rnd.intInRange(1, 50); // 18

rnd.setState(state); // Resumes previous state, regenerating same random sequence
rnd.intInRange(1, 50); // 39
rnd.intInRange(1, 50); // 24
rnd.intInRange(1, 50); // 18

The same sequence of operations can be repeated with equal results using the static methods of Srand:

Srand.seed(1836504610); // 1836504610 Set the seed 

Srand.inRange(0, 10); // 6.866552880965173
Srand.intInRange(0, 10); // 1

Srand.choice([1, 2, 3]); // 3
Srand.choices([1, 2, 3], 3); // [3, 3, 1] possible repetitions
Srand.choices([1, 2, 3], 3); // [2, 2, 3] possible repetitions

Srand.sample([1, 2, 3], 2); // [1, 2] no repetitions
Srand.sample([1, 2, 3], 2); // [1, 2] no repetitions

const state = Srand.getState();
Srand.intInRange(1, 50); // 39
Srand.intInRange(1, 50); // 24
Srand.intInRange(1, 50); // 18

Srand.setState(state); // Resumes previous state, regenerating same random sequence
Srand.intInRange(1, 50); // 39
Srand.intInRange(1, 50); // 24
Srand.intInRange(1, 50); // 18

API

Method Doc
choice(arr: Array<T>): T

Returns a random element from arr.

If arr is empty, an exception is thrown.

choices(arr: Array<T>, k: number): Array<T>

Returns a k-sized array sampled with replacement from arr, i.e. each element can be sampled more than once.

If k > 0 and arr is empty, throws an exception.

For an alternative without replacement, see sample.

getState(): State
Returns an object with the state of the generator.

Use setState to resume the state.

inRange(a: number, b: number): number

Returns a pseudo-random float number between a inclusive and b exclusive.

intInRange(min: number, max: number): number

Returns a psuedo-random integer between min and max inclusive.

noConflict(): Srand

Only available when using Srand as a plain script.

In the uncommon case the name Srand is already taken, restores its initial value and return the Srand object.

Srand = "my value";

// .. jsrand is loaded ...

const mySrand = Srand.noConflict();
Srand; // "my value"
random(): number

Returns a pseudo-random float number between 0 inclusive and 1 exclusive.

The algorithm used is a one-seeded version of the multiply-with-carry method by George Marsaglia.

randomize(): number
Sets and returns a random seed.
sample(arr: Array<T>, k: number): Array<T>

Returns a k-sized array sampled without replacement from arr.

If k > arr.length, an exception is thrown.

For an alternative with replacement, see choices.

seed(seed?: number): number
Sets or gets (if no argument is given) the seed.

The seed can be any float or integer number.

setState(state: State): void

Resume a state previously returned by getState.

shuffle(arr: Array<T>): Array<T>

Shuffles arr in-place using the Fisher-Yates algorithm and returns it (arr is modified).

License

Copyright © 2014-2020, Domenico De Felice.

Provided under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].