Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: x/exp/rand: give greater control over PCGSource #49934

DeedleFake opened this issue Dec 2, 2021 · 4 comments

proposal: x/exp/rand: give greater control over PCGSource #49934

DeedleFake opened this issue Dec 2, 2021 · 4 comments


Copy link

The current implementation of PCGSource has a number of missing features, mostly related to control over the seeding process. The Seed() method even has a TODO in it:

// Seed uses the provided seed value to initialize the generator to a deterministic state.
func (pcg *PCGSource) Seed(seed uint64) {
	pcg.low = seed
	pcg.high = seed // TODO: What is right?

Similarly, there's no way to get access to the current seed data.

Bikeshed Color 1

I propose adding a new function and two methods:

func NewPCGSource(high, low uint64) *PCGSource
func (pcg *PCGSource) SeedPCG(high, low uint64)
func (pcg *PCGSource) PCG() (high, low uint64)

This would give much greater control over the source.

Bikeshed Color 2

Alternatively, I recently copied the existing implementation in order to remove the internal state from it completely for a somewhat specific use case. I replaced the entire system with single function, func Next(high, low uint64) (n, nhigh, nlow uint64), that gave me full control over the generation.

If such a function was added, the existing implementation could easily be changed to just call any such function instead:

func (pcg *PCGSource) Uint64() (n uint64) {
  n, pcg.high, pcg.low = NextPCG(pcg.high, pcg.low)


I'm bikeshedding a bit, but my only point is that I'd like some greater control over the PCGSource implementation's seeding process.

@gopherbot gopherbot added this to the Proposal milestone Dec 2, 2021
@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Dec 2, 2021
Copy link

CC @robpike

Copy link

robpike commented Dec 3, 2021

The seeding methods seem reasonable. Moving to a Next model moves it further from the original math/rand than I'd like.

Send a CL? SeedPCG should probably be Seed64 so other sources could use it.

Copy link

Note that the PRNG state can be marshaled and unmarshaled which already provide these functionalities (though not directly and they require that the user knows the size of the seed fields — this could be noted in the documentation).

Copy link

Change mentions this issue: rand: add methods to allow greater control over PCGSource``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Status: Incoming

No branches or pull requests

5 participants