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

Iterator support #12

Closed TimothyGu closed 3 years ago

TimothyGu commented 5 years ago

It would seem to be nice for Math.seededPRNG() to return an iterator, so instead of .random() the user would instead call the more standard .next(). It would also be easier to reason about Math.seededPRNG() as a generator function.

tabatkins commented 5 years ago

Hahaha, I literally just changed away from a generator-based design.

I think that the current design makes more sense overall.

I encourage you to actually go thru some code examples that currently use Math.random() and see how they look with prng.random() vs an iterator.


As an aside, I consider the .next() method of generators to be, most of the time, an implementation detail, not an affirmatively-exposed part of your API. You rarely call next() directly in Python, for example. The {next,done} return value is inconvenient and weird to work with directly.

tabatkins commented 3 years ago

Closing, as I think copying the stateful design of Math.random() is definitely still the right way to go here, for the reasons listed above.