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

Alternative API #16

Open mfbx9da4 opened 3 years ago

mfbx9da4 commented 3 years ago

I think it would be nice to overload Math.random


function random(): number
function random(opts: EmptyObject): number
function random(opts: { seed: number }): RandomNumberGenerator

interface RandomNumberGenerator {
  next: () => number
  fork: () => RandomNumberGenerator 
}

type EmptyObject = Record<string, never>
mfbx9da4 commented 3 years ago

Also note fork here will create a new RNG which is predictably forked from the current one. ie does something like

…
fork = () => random({ seed: this.next() })
…