?
u
/
p
1-9
This is the preliminary formal specification for a proposed SeededPRNG
class
in JavaScript. It modifies the original ECMAScript specification with
several new or revised clauses. See the proposal's
explainer for the proposal's background, motivation, and usage examples.
A Seeded PRNG is an object representing a Pseudo-Random Number Generator; it holds a complex internal binary "state", and uses that to generate pseudo-random numbers on command, mutating its state as it does so. Two SeededPRNGs with the same "state" will generate the same sequence of numbers, allowing for reproducible "randomness".
Seeded PRNGs defined by this specification generate random numbers using the ChaCha 12 algorithm.
The SeededPRNG constructor:
This function performs the following steps when called.
SeededPRNG objects can be initialized in three ways:
The SeededPRNG constructor:
The SeededPRNG prototype object:
Unless explicitly stated otherwise, the methods of the SeededPRNG prototype object defined below are not generic and the
The initial value of SeededPRNG.prototype.constructor
is
The random()
method returns a pseudo-random value between 0 and 1 (including 0 but excluding 1), similar to Math.random()
.
It performs the following steps when called:
The GetPRNGValue() abstract operation takes a SeededPRNG [[PRNGState]] value,
and returns a pair of values,
consisting of the
It performs the following steps when called:
The randomSeed()
method returns a random seed value, suitable for initializing another SeededPRNG
object with. This method's return values cover the full possible range of 2128 seeds, unlike something like new SeededPRNG(prng.random())
, which only produces 253 possible seeds.
It performs the following steps when called:
The state()
method returns the internal state of the SeededPRNG
object, suitable for initializing another SeededPRNG
which will then produce an identical sequence of pseudo-random values.
It performs the following steps when called:
The setState()
method overwrites the internal state of the SeededPRNG
object with a new state.
It performs the following steps when called: