Skip to content

Latest commit

 

History

History
58 lines (39 loc) · 1.34 KB

File metadata and controls

58 lines (39 loc) · 1.34 KB

Best practices

Memorize function applications

replayable-random heavily rely on curried function, i.e. functions that have a single parameter and returns a new function.

We can take advantage of it to avoid unecessary computations and input validations. Indeed, we can memorize intermediate results.

For example we can turn the following code:

for (let i = 0; i < 1000; i++) {
    const n = distrib.mutU32Between(0)(5)(mutG)
    console.log(n)
}

into:

const f = distrib.mutU32Between(0)(5)
for (let i = 0; i < 1000; i++) {
    const n = f(mutG)
    console.log(n)
}

The inputs 0 and 5 are valided only once.

Seed generation

Some applications need to generate a seed for every new user. The seed should be human and computer friendely for easy exchange and storage.

To this end, Replayable- Random provides a distribution to generate array of base64 elements.

Generate a seed of 120 bits (6 bits * 20) using Math generator:

const seed = distrib.mutBase64Bytes(20)(Math)

Derive a first state from alea geneartor using this seed:

// Pure API
const g = alea.fromBytes(seed)

// Imperative API
const mutG = alea.mutFromBytes(seed)

Batch random generations

Coming soon...