How to seed the pcg random number generator?

1.1k Views Asked by At

In the samples for PCG they only seed one way which I assume is best/preferred practice:

pcg32 rng(pcg_extras::seed_seq_from<std::random_device>{});

or

// Seed with a real random value, if available
pcg_extras::seed_seq_from<std::random_device> seed_source;

// Make a random number engine 
pcg32 rng(seed_source);

However running this on my machine just produces the same seed every time. It is no better then if I just typed in some integer to seed with myself. What would be a good method to seed if trying it this way doesn't work ?

1

There are 1 best solutions below

0
On BEST ANSWER

pcg_extras::seed_seq_from is supposed to be the recommended way, but it delegates the actual seed generation to the generator specified in the template parameter.

MinGW has a broken implementation of std::random_device. So at this moment, if you want to target MinGW, you must not use std::random_device.

Some potential alternatives:

  • boost::random_device
  • randutils, by the author of PCG, M.E. O'Neill
  • seed11::seed_device, drop-in replacement for std::random_device (disclaimer: it's my own library)

More info about seeding in this blog post by M.E. O'Neill.