tc39 / proposal-seeded-random

Proposal for an options argument to be added to JS's Math.random() function, and some options to start it with.
MIT License
156 stars 6 forks source link

Making .seed a getter #13

Closed TimothyGu closed 5 years ago

TimothyGu commented 5 years ago

First, the explainer seems to be inconsistent on whether it's prng.seed() or prng.randomSeed().

Second, it seems to be more natural to make the seed/randomSeed property a getter instead. The operation of getting the seed seems to be fairly lightweight, and a getter would better convey that.

bakkot commented 5 years ago

If I understand correctly, .seed gets the current state of the PRNG, suitable for creating a clone of the current PRNG which will produce the same values, whereas .randomSeed gets a new seed every time it's invoked, suitable for creating a new PRNG which will produce different values. (These are confusingly similar, and one of them should probably be renamed. I'd vote for renaming .seed to .state.)

It seems reasonable for .seed to be a getter, but not .randomSeed.

TimothyGu commented 5 years ago

Good catch on the distinction. I agree with your points.

tabatkins commented 5 years ago

Hmmm, making .seed a getter would be reasonable.

I'm against using a non-"seed" word, because the whole point (of both of them) is that it's an appropriate value to use as a {seed} parameter of a new prng. Open to suggestions for better names under that constraint, tho.

tabatkins commented 5 years ago

Ooh, unexpected convenience of this switch: cloning a PRNG can now be done by just passing the parent PRNG as the argument, since it'll just pull the .seed property off of it:

const prng = Math.seededPRNG({seed:0});
const clone = Math.seededPRNG(prng);