PCG for Node.js
This is a wrapper module for the PCG random number generator.
PCG is not cryptographically secure, and it's got a couple other weaknesses, but it also has a few qualities that make it useful for games. Namely, it's fast, repeatable, and can easily jump to anywhere in its sequence given the seed and an index. Thus we can replay the random generation from any point in the sequence, which is helpful for game playback and logging of randomness.
Pull a specific integer from a sequence.
PCG needs four unsigned 32-bit integers as its seeds. (See Clarifications section below.) You can pass these four integers in along with an index to get that number in the sequence.
var pcg = ;var randomInteger = pcg;console; // will always output 3275776275
Note that the same seeds and index will always produce the same number. Make sure you advance the index every time you want a new random number.
You can get some initial seed values in all kinds of ways. To pull them from Node's approved randomness sources:
const crypto = ;var seed = crypto; // == 128 bits == four 32-bit integersvar dv = seedbuffer 0;var s1 = dv;var s2 = dv;var s3 = dv;var s4 = dv;var index = 0;pcg;index += 1; // (or you could do index++ in previous line)
Clarifications and caveats
Description of the seeds is slightly inaccurate, but close enough for what we care about. If you're curious as to what these values actually map to, check out the PCG C library documentation. The "seeds" here correspond to the low and high bits of the seed and sequence.
There is a mild decline in performance as the index climbs higher; in my testing on a 2.6 GHz Intel Core i5, it amounted to a few hundred nanoseconds at the worst. Safely negligible in most contexts, but something to be aware of.
- The biggest dip in performance comes as the index crosses the 32-bit limit; doing the work to convert strings to integers isn't free.
The index is presently limited to the range of 64-bit unsigned integers, and you'll get an exception if you exceed that range.