Coder Social home page Coder Social logo

seedrandom's Introduction

seedrandom.js

Build Status NPM version Bower version

Seeded random number generator for JavaScript.

Version 3.0.5

Author: David Bau

Date: 2019-09-14

Can be used as a plain script, a Node.js module or an AMD module.

Script tag usage

<script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/seedrandom.min.js">
</script>
// Make a predictable pseudorandom number generator.
var myrng = new Math.seedrandom('hello.');
console.log(myrng());                // Always 0.9282578795792454
console.log(myrng());                // Always 0.3752569768646784

// Use "quick" to get only 32 bits of randomness in a float.
console.log(myrng.quick());          // Always 0.7316977467853576

// Use "int32" to get a 32 bit (signed) integer
console.log(myrng.int32());          // Always 1966374204

// Calling seedrandom with no arguments creates an ARC4-based PRNG
// that is autoseeded using the current time, dom state, and other
// accumulated local entropy.
var prng = new Math.seedrandom();
console.log(prng());                // Reasonably unpredictable.

// Seeds using the given explicit seed mixed with accumulated entropy.
prng = new Math.seedrandom('added entropy.', { entropy: true });
console.log(prng());                // As unpredictable as added entropy.

// Warning: if you call Math.seedrandom without `new`, it replaces
// Math.random with the predictable new Math.seedrandom(...), as follows:
Math.seedrandom('hello.');
console.log(Math.random());          // Always 0.9282578795792454
console.log(Math.random());          // Always 0.3752569768646784

Note: calling Math.seedrandom('constant') without new will make Math.random() predictable globally, which is intended to be useful for derandomizing code for testing, but should not be done in a production library. If you need a local seeded PRNG, use myrng = new Math.seedrandom('seed') instead. For example, cryptico, an RSA encryption package, uses the wrong form, and thus secretly makes Math.random() perfectly predictable. The cryptico library (and any other library that does this) should not be trusted in a security-sensitive application.

Other Fast PRNG Algorithms

The package includes some other fast PRNGs. To use Johannes Baagøe's extremely fast Alea PRNG:

<script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/lib/alea.min.js">
</script>
// Use alea for Johannes Baagøe's clever and fast floating-point RNG.
var arng = new alea('hello.');

// By default provides 32 bits of randomness in a float.
console.log(arng());               // Always 0.4783254903741181

// Use "double" to get 56 bits of randomness.
console.log(arng.double());        // Always 0.8297006866124559

// Use "int32" to get a 32 bit (signed) integer.
console.log(arng.int32());         // Always 1076136327

Besides alea, there are several other faster PRNGs available. Note that none of these fast PRNGs provide autoseeding: you need to provide your own seed (or use the default autoseeded seedrandom to make a seed).

PRNG name Time vs native Period Author
alea 1.95 ns, 0.9x ~2^116 Baagøe
xor128 2.04 ns, 0.9x 2^128-1 Marsaglia
tychei 2.32 ns, 1.1x ~2^127 Neves/Araujo (ChaCha)
xorwow 2.40 ns, 1.1x 2^192-2^32 Marsaglia
xor4096 2.40 ns, 1.1x 2^4096-2^32 Brent (xorgens)
xorshift7 2.64 ns, 1.3x 2^256-1 Panneton/L'ecuyer
quick 3.80 ns, 1.8x ~2^1600 Bau (ARC4)

(Timings were done on node v0.12.2 on a single-core Google Compute Engine instance. quick is just the 32-bit version of the RC4-based PRNG originally packaged with seedrandom.)

CJS / Node.js usage

npm install seedrandom
// Local PRNG: does not affect Math.random.
var seedrandom = require('seedrandom');
var rng = seedrandom('hello.');
console.log(rng());                  // Always 0.9282578795792454

// Global PRNG: set Math.random.
seedrandom('hello.', { global: true });
console.log(Math.random());          // Always 0.9282578795792454

// Autoseeded ARC4-based PRNG.
rng = seedrandom();
console.log(rng());                  // Reasonably unpredictable.

// Mixing accumulated entropy.
rng = seedrandom('added entropy.', { entropy: true });
console.log(rng());                  // As unpredictable as added entropy.

// Using alternate algorithms, as listed above.
var rng2 = seedrandom.xor4096('hello.')
console.log(rng2());

Starting in version 3, when using via require('seedrandom'), the global Math.seedrandom is no longer available.

Require.js usage

Similar to Node.js usage:

bower install seedrandom
require(['seedrandom'], function(seedrandom) {
  var rng = seedrandom('hello.');
  console.log(rng());                  // Always 0.9282578795792454
});

Network seeding

<script src=//cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/seedrandom.min.js>
</script>
<!-- Seeds using urandom bits from a server. -->
<script src=//jsonlib.appspot.com/urandom?callback=Math.seedrandom>
</script>

<!-- Seeds mixing in random.org bits -->
<script>
(function(x, u, s){
  try {
    // Make a synchronous request to random.org.
    x.open('GET', u, false);
    x.send();
    s = unescape(x.response.trim().replace(/^|\s/g, '%'));
  } finally {
    // Seed with the response, or autoseed on failure.
    Math.seedrandom(s, !!s);
  }
})(new XMLHttpRequest, 'https://www.random.org/integers/' +
  '?num=256&min=0&max=255&col=1&base=16&format=plain&rnd=new');
</script>

Reseeding using user input

var seed = Math.seedrandom();        // Use prng with an automatic seed.
document.write(Math.random());       // Pretty much unpredictable x.

var rng = new Math.seedrandom(seed); // A new prng with the same seed.
document.write(rng());               // Repeat the 'unpredictable' x.

function reseed(event, count) {      // Define a custom entropy collector.
  var t = [];
  function w(e) {
    t.push([e.pageX, e.pageY, +new Date]);
    if (t.length < count) { return; }
    document.removeEventListener(event, w);
    Math.seedrandom(t, { entropy: true });
  }
  document.addEventListener(event, w);
}
reseed('mousemove', 100);            // Reseed after 100 mouse moves.

The "pass" option can be used to get both the prng and the seed. The following returns both an autoseeded prng and the seed as an object, without mutating Math.random:

var obj = Math.seedrandom(null, { pass: function(prng, seed) {
  return { random: prng, seed: seed };
}});

Saving and Restoring PRNG state

var seedrandom = Math.seedrandom;
var saveable = seedrandom("secret-seed", {state: true});
for (var j = 0; j < 1e5; ++j) saveable();
var saved = saveable.state();
var replica = seedrandom("", {state: saved});
assert(replica() == saveable());

In normal use the prng is opaque and its internal state cannot be accessed. However, if the "state" option is specified, the prng gets a state() method that returns a plain object the can be used to reconstruct a prng later in the same state (by passing that saved object back as the state option).

Version notes

The random number sequence is the same as version 1.0 for string seeds.

  • Version 2.0 changed the sequence for non-string seeds.
  • Version 2.1 speeds seeding and uses window.crypto to autoseed if present.
  • Version 2.2 alters non-crypto autoseeding to sweep up entropy from plugins.
  • Version 2.3 adds support for "new", module loading, and a null seed arg.
  • Version 2.3.1 adds a build environment, module packaging, and tests.
  • Version 2.3.4 fixes bugs on IE8, and switches to MIT license.
  • Version 2.3.6 adds a readable options object argument.
  • Version 2.3.10 adds support for node.js crypto (contributed by ctd1500).
  • Version 2.3.11 adds an option to load and save internal state.
  • Version 2.4.0 adds implementations of several other fast PRNGs.
  • Version 2.4.2 adds an implementation of Baagoe's very fast Alea PRNG.
  • Version 2.4.3 ignores nodejs crypto when under browserify.
  • Version 2.4.4 avoids strict mode problem with global this reference.
  • Version 3.0.1 removes Math.seedrandom for require('seedrandom') users.
  • Version 3.0.3 updates package.json for CDN entrypoints.
  • Version 3.0.5 removes eval to avoid triggering content-security policy.

The standard ARC4 key scheduler cycles short keys, which means that seedrandom('ab') is equivalent to seedrandom('abab') and 'ababab'. Therefore it is a good idea to add a terminator to avoid trivial equivalences on short string seeds, e.g., Math.seedrandom(str + '\0'). Starting with version 2.0, a terminator is added automatically for non-string seeds, so seeding with the number 111 is the same as seeding with '111\0'.

When seedrandom() is called with zero args or a null seed, it uses a seed drawn from the browser crypto object if present. If there is no crypto support, seedrandom() uses the current time, the native rng, and a walk of several DOM objects to collect a few bits of entropy.

Each time the one- or two-argument forms of seedrandom are called, entropy from the passed seed is accumulated in a pool to help generate future seeds for the zero- and two-argument forms of seedrandom.

On speed - This javascript implementation of Math.random() is several times slower than the built-in Math.random() because it is not native code, but that is typically fast enough. Some details (timings on Chrome 25 on a 2010 vintage macbook):

  • seeded Math.random() - avg less than 0.0002 milliseconds per call
  • seedrandom('explicit.') - avg less than 0.2 milliseconds per call
  • seedrandom('explicit.', true) - avg less than 0.2 milliseconds per call
  • seedrandom() with crypto - avg less than 0.2 milliseconds per call

Autoseeding without crypto is somewhat slow, about 20-30 milliseconds on a 2012 windows 7 1.5ghz i5 laptop, as seen on Firefox 19, IE 10, and Opera. Seeded rng calls themselves are fast across these browsers, with slowest numbers on Opera at about 0.0005 ms per seeded Math.random().

LICENSE (MIT)

Copyright 2019 David Bau.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

seedrandom's People

Contributors

automata avatar bluelovers avatar dave-5 avatar davidbau avatar jared-stilwell avatar louh avatar mreinstein avatar timothygu avatar tmcw avatar zjonsson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

seedrandom's Issues

TypeError: Object #<Object> has no method 'seedrandom'

(Thanks again for an awesome project!)

My usage is really simple:

<script src="http://cdnjs.cloudflare.com/ajax/libs/seedrandom/2.3.11/seedrandom.min.js"></script>

Then I do:
Math.seedrandom(...)

It works really well, but I got an email with a JS error:
TypeError: Object # has no method 'seedrandom'
(I automatically send myself errors if they happened in production.)

The file seedrandom.min.js was definitely loaded because I use HTML5 manifest, so either everything loads or nothing loads :)

I really have no idea how to reproduce it, but I wanted to record the issue in case someone else runs into it in the future.

It happened for this settings:
URL: http://yoav-zibin.github.io/multiplayer-games/index.html#/main
Navigator:
appName=Netscape,
appCodeName=Mozilla,
platform=Linux armv7l,
appVersion=5.0 (Linux; Android 4.4.2; Endeavour_101L Build/KVT49L) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Safari/537.36,
product=Gecko,
userAgent=Mozilla/5.0 (Linux; Android 4.4.2; Endeavour_101L Build/KVT49L) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Safari/537.36,
vendor=Google Inc.

Compatibility with browserify

Hi, I am using this module to generate a proceduraly generated world in a 3D game project using three.js. I am very glad i found this module since i was breaking my head over seeded randoms.

The only downside i am experiencing is that this doesn't seem to be compatible with browserify.
browserify enables me to use "require();" in browser code. It seems your module provides this functionality to, using require.js?

Anyway when i try to build my project with browserify i get the "can not find module" error.

As soon as i find a solution, i'll post it here.

NPM package does not provide an ECMAScript module

Context: I want to use TensorFlow.js in the browser. I'm using rollup to bundle my app for the browser.

The TensorFlow.js source imports from seedrandom in several places. This comes through in the ES modules distributed via npm.

To find the module seedrandom imported by TensorFlow, I'm using the plugin @rollup/plugin-node-resolve. This does find the node_modules/seedrandom package.

Unfortunately, @rollup/plugin-node-resolve does not find an ECMAScript module in the seedrandom package. So instead, it decides to bundle the seedrandom index.js. But this is a CommonJS module, not an ES module.

As a result, rollup gives me errors like alea is not exported by node_modules/seedrandom/index.js, and the browser gives me errors like require is not defined.

I think the right solution is for the seedrandom package to provide an ES module. The alternative is for TensorFlow to stop importing from a package that doesn't provide an ES module.

(You might say "well you just need to use a CommonJS->ES transpiler, like @rollup/plugin-commonjs". I'm reluctant to do so, because this transformation is gross, and in general, impossible.)

Allow undefined to be passed as an argument to randomly seed

Instead of being able to do

Math.seedrandom(specialCase ? specialSeed : undefined);

where I might want it to be randomly seeded, but in a special case I want a particular seed, I currently need to write

if (specialCase) Math.seedrandom(specialSeed);
else Math.seedrandom()

I had expected undefined to work the same way as no argument at all, but it is being converted to a string undefined\0. Maybe null should work the same way too.

The second code sample isn't much more complicated, I concede, but the first is more concise and I fully expected it to work.

`seedrandom.alea()` not working without seed

Is it intended that alea() doesn't work without passing a seed? Calling it without arguments produces an error.

Test code (tested on npm.runkit.com using version 2.4.3):

var seedrandom = require("seedrandom");
var rng = seedrandom.alea();
TypeError: Cannot read property 'toString' of undefined

geneare number between a range

How can i use "seedrandom" to generate a random number between a specific range.

For example: generate number between "0" - "20". I do not want float numbers.

Thank you for any help.

Same seed, different result on different browsers

Tested on a Mac with the same seed of course:
-when using 2 instances of Chrome, the result of a randomly altered render in a canvas is exactly the same, even after 12000 loops.
-when using 1 instance of Chrome and 1 instance of Safari, the result is different. Chrome is the same as above, Safari is totally different.

So I assume the results depend on the browser / version / js engine.
My question:
Is this a fixable bug, an undesired but unavoidable bug, or a feature?

Thanks and thanks for the library.
l

Should not override Math.random

I see that you enclosed your code in a self-executing anonymous function, so I believe that you are aware of problems with global scope polution.

Even if this is a 99% of use-cases, I would never say that overwriting the Math.random globally is a good idea.
Observe that adding a single line

Math.random = seedrandom();

would not be complicated for these 99% users, while adding

var realRandom=Math.random;
Math.seedrandom(...);
var newRandom=Math.random;
Math.random=realRandom;

is a bit complicated and not even guaranteed to work for this 1%.

My usecase is that I am not sure what other widgets are actually using Math.random for and I wouldn't like to interefere with their execution.

On RC4 based PRNGs

Hey David,

I have used seedrandom in a project about two years ago. I just came across it again and noticed that you're still actively maintaining it. This kind of surprises me considering that it is based on the RC4 / ARC4 / Arcfour stream cipher which is being considered insecure.

It is my understanding that a CSPRNG based on an insecure cipher results in an insure CSPRNG. Without looking too deep into cryptographic papers, I for example found:
http://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator#Designs_based_on_cryptographic_primitives and
http://en.wikipedia.org/wiki/RC4#RC4-based_random_number_generators

So it looks like using a PRNG based on a different design, e.g. Fortuna, would be a better idea security wise. Maybe that is a misunderstanding on my side you can clear up.

Thanks a lot.

Cheers,
Andreas

Problem with state

For some reasons the code bellow returns me an error:

  const seedrandom = require('seedrandom');
  const rng = new seedrandom('seed', { saved: true })
  console.log(rng())
  console.log(rng.state())

the error:
TypeError: rng.state is not a function
and my environment:

node -v
v10.6.0

#33 – I read that answer and added new. It doesn't help

Why doesn't this work with lodash?

If I do a simple test like

Math.seedrandom(1987598823)
console.log(_.random(999))

It ends up producing different numbers every time. Whereas if I use underscore or just Math.random it produces a consistent number, as expected. Lodash simply says nativeRandom = Math.random; and then uses nativeRandom throughout for its randomness. I call Math.seedrandom before loading lodash and it still produces inconsistent results.

Undesirable to override/overwrite Math.random

As far as I can see, this library overrides or overwrites the Math.random() function.
I don't really see the point. This can very easily be done by the user of this library if desired, but has a lot of potential problems for every other user.

I'd much rather see this library be stand-alone and not monkey-patch the Math object.

Question

Hello davidbau, great lib.

I'm adding noise to a graph and i need to be the same any time i run the script, the only thing is that i need to jump values when the graph is too long, for example:

Math.seedrandom('test');
console.log(Math.random());  // 0.8722025543160253 
console.log(Math.random());  // 0.4023928518604753 
console.log(Math.random());  // 0.9647289658507073
console.log(Math.random());  // 0.30479896375101545 
//... Then i get a lot more

I need to do something like

Math.seedrandom('test');
Math.skipRandoms(3);
console.log(Math.random()); // 0.30479896375101545

Is something like this possible?

Thanks for this awesome lib 👍

[question] reverse PRNG

Would it be possible to have an input of random numbers in correct order and get an output what is the seed?

Use of `eval` is unsafe and is disabled on some servers

Hi @davidbau, thanks for your work on this package. We'd like to replace our home-grown pseudo-random number generator with your package, but ran into an issue with the use of eval (prior art: issue #46). Allowing eval() is unsafe and for servers that set strict Content Security Policies for scripts, code with eval will not be permitted to run.

The Universal Module Definition uses self to reach the global context in regardless of strict mode in browsers, and this otherwise (for Node). (https://github.com/umdjs/umd/blob/master/templates/returnExportsGlobal.js#L32)

For example:

function a() { console.log(this) }
// a() -> Window

function b() { 'use strict'; console.log(this); }
// b() -> undefined

function c() { 'use strict'; console.log(self); }
// c() -> Window

Would you consider replacing the eval code so that we can use your package in environments where eval is disallowed?

Thanks!

Saved state doesn't work

I can't even run the example in the documentation:

var seedrandom = Math.seedrandom;
var saveable = seedrandom("secret-seed", {state: true});
for (var j = 0; j < 1e5; ++j) saveable();
var saved = saveable.state();
var replica = seedrandom("", {state: saved});
assert(replica() == saveable());

I get an error: Uncaught TypeError: saveable.state is not a function(…)

seedrandom is creating massive bundles when used with browserify

I'm trying to seedrandom in browserify. The result works but creates a gigantic amount of javascript that gets put into the bundle.

My bundle size before seedrandom: 17975 bytes. after: 618002 bytes.

I this is happening because of checks like this: https://github.com/davidbau/seedrandom/blob/released/seedrandom.js#L232

browserify is technically runnning in a node environment so it includes this module, but then it ends up shimming the node crypto module for use in the browser, which isn't needed at all.

would love to figure out how we could solve this, as it seems like a high quality rng module but I can't inflate my front end javascript from 18k -> 618k.

thanks!

global undefined

You define the global variable as this.

var global = this,

This doesn't work in many contexts (particularly if inside of a "use strict"); and infact makes your use of global uninferable by JS tooling.

Strings consisting of a repeated pattern cause collisions

I've found that using strings that consist of repeating patterns cause the number generator to return identical results.

For example, suppose that you seed seedrandom with a string 'sfsf'. Then seed it again with 'sfsfsf'. Both number generators will produce 0.12427751372875664 on the first call.

In Node.js:

let rngA = seedrandom('sf');
let a = rngA();

let rngB = seedrandom('sfsf');
let b = rngB();

assert(a !== b);

It doesn't seem to matter how many times the pattern repeats. I could use 'sfsfsf' or 'sfsfsfsfsfsf' and get the same result.

I would expect these to be totally different values because they are different strings.

Persisting a seedrandom instance

Hi,

Is it possible to serialize a seedrandom instance, and then restore it later?

My use case is that I'm using it to procedurally generate levels in a game, and allowing the user to pick the seed. It works great, except that if the user saves and quits, I can't find a way to save and restore the state, so the random numbers will be wrong, as compared to having retained the same instance.

For example, I want this:

var rng = seedrandom("Foobar");
console.log(rng()); //0.12345

var state = rng.serialize();

console.log(rng()); //0.23456
console.log(rng()); //0.34567

rng = seedrandom.unserialize(state); // or, seedrandom(state) or whatever

console.log(rng()); //0.23456
console.log(rng()); //0.34567

document the range or results

(hi, thanks for this library - very excited to be able to seed things, so e.g. if i try to make a game i can experiment with replaying things by reusing a seed.)

it would be great for a newbie coming to this library for the return values to be documented e.g. i /assume/ that double() returns something in the range 0...1, because that's often how other prng's work, but it isn't explicit that i could see in e.g. the readme. people like me who are easily confused would get a leg up with such clarifications. :-)

inverted Math.round() result

Idk if it really is an issue but i dont understand this behavior:

var rng = seedrandom('hello');
let i = rng.quick()

var rng2 = seedrandom('hello1');
let i2 = rng2.quick()

console.log(i);
console.log(Math.round(i));
console.log(Math.round(rng.quick()));

console.log(i2);
console.log(Math.round(i2));
console.log(Math.round(rng2.quick()));

results in :

0.5463663768023252
1
0
0.4120498853735626
0
1

same behavior with rng().
anyone can explain this to me?
using v 3.0.5

Thanks

Unbiased float range

How can I achieve an unbiased float number between a range of two numbers, with a precision of 8 decimals ?

Let's say I want a number between 0.00010000 and 10,00000000 (ten)
I could use: `(Math.floor(prng() * (1000000000 - 10000) + 10000) ) / 100000000;

Are the results of this unbiased ?

Seedrandom is slow

Seedrandom is careful, but slow. It is about 9x slower than native Math.random(), which is acceptable if you don't need too much randomness. But if you need a lot of randomness, you want a seedable prng about the same speed as Math.random().

A possible solution is to offer alternate algorithms that get closer to native random such as:
https://github.com/davidbau/seedrandom/tree/xorshift/xor

Integer range specification

seedrandom('seed').int32(0, 5) should return a value between 0 (inclusive) and 5 (exclusive): 0, 1, 2, 3, 4. I have already implemented the functionality myself, but feel that this should be a built-in method.

/**
 * A randomization engine which can be made deterministic by using a
 * custom-seeded engine.
 */
export default class Randomizer {
  /**
   * Engine to be used for randomization.
   * @type {Object}
   */
  engine;

  constructor(engine) {
    this.engine = engine;
  }

  /**
   * Returns a random 32-bit integer (signed or unsigned) in the given range.
   * @param {number} min Minimum value (included).
   * @param {number} max Maximum value (excluded).
   * @returns {number}
   */
  getInt(min, max) {
    const range = max - min;

    // Get the lowest integer which is not part of the equal distribution range
    const firstTooHighValue =
      Config.UINT32_RANGE - Config.UINT32_RANGE % range;

    let result;
    do {
      // Generate a random 32-bit unsigned integer
      result = this.engine.int32() - Config.INT32_MIN_VALUE;
    } while (
      // Ensure equal distribution
      result >= firstTooHighValue
    );

    // Place the result in the range and offset it by the minimum value
    return result % range + min;
  }

  /**
   * Shuffles the elements of an array.
   * @param {Object[]} array Array to be shuffled.
   * @returns {Object[]}
   */
  shuffleArray(array) {
    const result = [...array];

    // Perform Durstenfeld shuffle
    for (let i = array.length - 1; i > 0; --i) {
      // Generate a random integer in [0, i] deterministically
      const j = this.getInt(0, i);

      // Swap result[i] and result[j]
      [result[i], result[j]] = [result[j], result[i]];
    }

    return result;
  }
}

flatten() implementation produces warnings in the browser javascript console

here's what I see:

screen shot 2017-02-09 at 1 01 58 pm

Here is the specific line that's causing the warnings: https://github.com/davidbau/seedrandom/blob/released/seedrandom.js#L169

I'm not sure why but flatten() seems to be getting called on the window object. It's iterating over pretty much everything in the entire dom, including some of these deprecated properties.

I'm thinking if this could be fixed, it would not only remove the warnings but probably improve performance as I'm sure iterating over everything attached to the window must be somewhat costly.

thoughts?

production bytesize impact?

How will this impact my production build? I'm guessing hardly but it'd be nice to have this statistic somewhere in the README.

compatibility with react-native

uncaught error Error: UnableToResolveError: Unable to resolve module crypto from /Users/gre/dev/gl-react-native/Examples/Hearts/node_modules/seedrandom/seedrandom.js: Invalid directory /Users/node_modules/crypto

this is what happens in React Native, there should be a better way to detect node environment than a try catch covering the require("crypto") ?

Question about seeds and collisions

I am wondering how many unique sequences are possible, and the likelihood of collisions given many different seeds.

For instance, seeding a PRNG with all possible 128-bit numbers. Will that yield 2^128 unique number sequences? Or would there be collisions such that two different seeds would produce the same sequence of numbers? Is it related to the 'period' at all? quick has a period of ~2^1600, for example.

The reason why I am asking: I'm thinking of using seedrandom in a pattern generator, and seeding it with a hash (64-bit or 128-bit). The patterns generated would be highly variable, but if the initial seed is a limiting factor, it could greatly reduce the number of possible patterns. I'm hoping that there would be as many outcomes as the hash size I've provided as a seed (2^64 or 2^128).

In another way of putting it, do PRNGs only use x bits of the provided seed? Like if I provide a 128-bit hash string it would only utilize a portion of it. Like an AND mask (e.g. only using 16-bits of 32-bit input: 0xFFFFFFFF & 0xFFFF).

Hope that makes sense.

Clean up

Hi,

The seedrandom.js, is, for a lack of a better word, a freaking maze.

Comments

First, the extra-long comment in the beginning of the file that takes literally more than half the file is already covered completely in README.md. Why spend the time to duplicate it in the source?

Second, the intermixing of (useless) jsdoc comments with // comments is extremely unpleasant to the eye.

/**
 * All code is in an anonymous closure to keep the global namespace clean.
 */

... OK? This is not JSDoc at all.

Then there is

/** @constructor */
function ARC4(key) {
  [...]
}

Yes, we know, it's a constructor.

Closure

Then, there is the closure:

(function (
    global, pool, math, width, chunks, digits, module, define, rngname) {
[...]
})(
  this,   // global window object
  [],     // pool: entropy pool starts empty
  Math,   // math: package containing random, pow, and seedrandom
  256,    // width: each RC4 output is 0 <= x < 256
  6,      // chunks: at least six RC4 outputs for each double
  52,     // digits: there are 52 significant digits in a double
  (typeof module) == 'object' && module,    // present in node.js
  (typeof define) == 'function' && define,  // present with an AMD loader
  'random'// rngname: name for Math.random and Math.seedrandom
);

Why not just declare all of them in the closure itself? This is not making it customizable or anything.

Node.js integration

  try {
    // When in node.js, try using crypto package for autoseeding.
    nodecrypto = require('crypto');
  } catch (ex) {}

Is there a reason why this is at the end of the file, ~200 lines from var nodecrypto declaration?

If you want, I volunteer to fix all these issues. I just thought that the code is utterly unreadable.

Custom integral number type support

Add the following methods to seedrandom:

  • .int(bytes = 4, unsigned = false, min = 0, max = 256 ** bytes - 1)
  • .int8(min = -128, max = 127)
  • .uint8(min = 0, max = 255)
  • .int16(min = -32768, max = 32767)
  • .uint16(min = 0, max = 65535)
  • .uint32(min = 0, max = 4294967295)

Compatibility with browserify in Node.js

I tried to use seedrandom in Node.js and browerify, but got error like this, (“xxx” means my workspace path)

Error: Cannot find module '../../../../.1.1.4@is-buffer/index.js' from 'xxx/node_modules/.1.0.2@core-util-is/lib'
at xxx/node_modules/.1.1.7@resolve/lib/async.js:55:21
at load (xxx/node_modules/.1.1.7@resolve/lib/async.js:69:43)
at onex (xxx/node_modules/.1.1.7@resolve/lib/async.js:92:31)
at xxx/node_modules/.1.1.7@resolve/lib/async.js:22:47
at FSReqWrap.oncomplete (fs.js:123:15)

So I direct use seedrandom.js file, but got the same error, unless I comment blow lines:
image

Seems require('crypto') not Compatibility with browserify? Anyway, it works now..

OS: Linux VM_47_16_centos 3.10.0-327.el7.x86_64
node: v6.9.1
npm: 3.10.9
browerify: 13.1.1

tests are failing

here's what I get:

λ git clone [email protected]:mreinstein/seedrandom.git
λ cd seedrandom
λ npm i
λ npm test

> [email protected] test /Users/michaelreinstein/wwwroot/seedrandom
> grunt travis

Running "uglify:all" (uglify) task
File seedrandom.min.js created: 8.24 kB → 1.62 kB
File lib/alea.min.js created: 3.24 kB → 986 B
File lib/tychei.min.js created: 2.52 kB → 871 B
File lib/xor4096.min.js created: 4.56 kB → 1.15 kB
File lib/xorshift7.min.js created: 2.42 kB → 988 B
File lib/xorwow.min.js created: 1.92 kB → 837 B
File lib/xor128.min.js created: 1.75 kB → 747 B
>> 7 files created.

Running "sed:nullchar" (sed) task

Running "qunit:all" (qunit) task
Testing test/altprng.html 
PhantomJS threw an error:ERROR
>> 0 [ '' ]
Warning: PhantomJS exited unexpectedly with exit code null. Use --force to continue.

Aborted due to warnings.

λ

Unset seedrandom

Hello,

seedrandom is super awesome, but I have one issue.

How do I unset a global seed of Math.random()?

For example, say I need Math.random() seeded for a little bit so i use seedrandom('hello', {global: true});

How do I unset it?

Thank you

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.