项目作者: DomenicoDeFelice

项目描述 :
A seeded pseudo-random number generator for JavaScript.
高级语言: JavaScript
项目地址: git://github.com/DomenicoDeFelice/jsrand.git
创建时间: 2014-02-21T18:44:08Z
项目社区:https://github.com/DomenicoDeFelice/jsrand

开源协议:MIT License

下载


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

  1. $ 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 scriptNPM


javascript <script src="jsrand.min.js"></script>

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



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

or

Javascript import Srand from 'seeded-rand';

All methods can be used either statically:

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

or instantiating a new generator:

  1. const rnd = new Srand(10);
  2. rnd.random(); // 0.4569510892033577
  3. const othr = new Srand(rnd.seed());
  4. othr.random(); // 0.4569510892033577

Examples

  1. const rnd = new Srand(); // Initiate with random seed
  2. rnd.seed(); // 1836504610 Read the seed
  3. rnd.randomize(); // 3409024789 Random seed is set and returned
  4. rnd.seed(1836504610); // 1836504610 Set a seed
  5. rnd.inRange(0, 10); // 6.866552880965173
  6. rnd.intInRange(0, 10); // 1
  7. rnd.choice([1, 2, 3]); // 3
  8. rnd.choices([1, 2, 3], 3); // [3, 3, 1] possible repetitions
  9. rnd.choices([1, 2, 3], 3); // [2, 2, 3] possible repetitions
  10. rnd.sample([1, 2, 3], 2); // [1, 2] no repetitions
  11. rnd.sample([1, 2, 3], 2); // [1, 2] no repetitions
  12. const state = rnd.getState();
  13. rnd.intInRange(1, 50); // 39
  14. rnd.intInRange(1, 50); // 24
  15. rnd.intInRange(1, 50); // 18
  16. rnd.setState(state); // Resumes previous state, regenerating same random sequence
  17. rnd.intInRange(1, 50); // 39
  18. rnd.intInRange(1, 50); // 24
  19. rnd.intInRange(1, 50); // 18

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

  1. Srand.seed(1836504610); // 1836504610 Set the seed
  2. Srand.inRange(0, 10); // 6.866552880965173
  3. Srand.intInRange(0, 10); // 1
  4. Srand.choice([1, 2, 3]); // 3
  5. Srand.choices([1, 2, 3], 3); // [3, 3, 1] possible repetitions
  6. Srand.choices([1, 2, 3], 3); // [2, 2, 3] possible repetitions
  7. Srand.sample([1, 2, 3], 2); // [1, 2] no repetitions
  8. Srand.sample([1, 2, 3], 2); // [1, 2] no repetitions
  9. const state = Srand.getState();
  10. Srand.intInRange(1, 50); // 39
  11. Srand.intInRange(1, 50); // 24
  12. Srand.intInRange(1, 50); // 18
  13. Srand.setState(state); // Resumes previous state, regenerating same random sequence
  14. Srand.intInRange(1, 50); // 39
  15. Srand.intInRange(1, 50); // 24
  16. Srand.intInRange(1, 50); // 18

API





































































MethodDoc


Javascript choice(arr: Array<T>): T



Returns a random element from arr.

If arr is empty, an exception is thrown.


Javascript 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.


Javascript getState(): State


Returns an object with the state of the generator.

Use setState to resume the state.


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



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


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



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


Javascript 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.

javascript Srand = "my value"; // .. jsrand is loaded ... const mySrand = Srand.noConflict(); Srand; // "my value"



Javascript 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.


Javascript randomize(): number


Sets and returns a random seed.


Javascript 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.


Javascript seed(seed?: number): number


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

The seed can be any float or integer number.


Javascript setState(state: State): void



Resume a state previously returned by getState.


Javascript 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.