davidbau / seedrandom

seeded random number generator for Javascript
2.04k stars 160 forks source link

`seedrandom.alea()` not working without seed #50

Closed Neonit closed 6 years ago

Neonit commented 6 years ago

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
jeffrose commented 6 years ago

When no seed is passed in data is undefined.

https://github.com/davidbau/seedrandom/blob/released/lib/alea.js#L82

It should probably be set to empty string in that scenario.

Neonit commented 6 years ago

As user of the library, I'd expect it to choose a random seed. At least something like Date.now().toString(). Or the same that Math.seedrandom() does. I'd say defaulting to an empty string is rather counter-intuitive and maybe even dangerous (because users wouldn't expect it to be the same every time).

jeffrose commented 6 years ago

It looks like there's an autoseed function for the default algorithm but not in the additional ones, i.e. alea, xor128, etc.

https://github.com/davidbau/seedrandom/blob/released/seedrandom.js#L194

Neonit commented 6 years ago

Yes. What I wanted to suggest was to set alea (and others) to use a random seed from autoseed (or elsewhere) instead of setting it to use an empty string, if no seed is given.

davidbau commented 6 years ago

Autoseeding has lots of (hidden security) implications, and I've never been very happy with the original seedrandom's (slow, not very secure) autoseeding.

The idea is that it should throw an exception so that programmers know they need to supply a seed.