philihp / pcg

A functional implementation of the PCG family random number generators, written in TypeScript.
https://www.npmjs.com/package/pcg
MIT License
3 stars 0 forks source link

serializing state to resume generation from the same point #179

Open BrianHicks opened 3 months ago

BrianHicks commented 3 months ago

Hi! I'm interested in using this library in a local-first application to generate data in a consistent way across different clients. Because of that, I need to be able to serialize the state somehow to reuse later. I've used other implementations of PCG where this is possible, but I don't see a clear way to do it here—if you don't mind my asking, how would you recommend approaching this?

philihp commented 2 months ago

Ohh, I can see how that would be useful.

I've been meaning to do a bigger overhaul of this library for another major version. I'd be happy to include that. Would you be able to share how you're using it (or any similar library)?

BrianHicks commented 2 months ago

Well, to level-set, this is just in a hobby project, and I don't want to appear to be making demands!

But essentially, I'm making something similar to TagTime. For that to work, the checkins ("pings") are randomly timed, but the overall spacing needs to follow a particular distribution.

To add another wrinkle onto this, I'm storing all the data in an automerge document, so I don't have the benefit of a central server to coordinate inserting pings.

Given the constraints, PCG seems like a great option for this: I can hypothetically store/restore the state information to get consistent results across different clients (and possibly different languages.) That's not the only way to do it, though: having a consistently-seedable implementation means I can also use the timestamps of the pings to get a repeatable source of randomness. 👍