0

I would like to generate 64 bit integers using the standard c++ std::mt19937_64 without manually seeding it. When defining the seed though, this question occurred to me: is there a proper way of seeding such a random generator using the current time (eg. producing a number based on the current time point, that is at least as big as a 64 bit int)? Something like counting milliseconds from the Unix epoch? Is there an easy way to achieve this?

Nicol Bolas
  • 378,677
  • 53
  • 635
  • 829
Adam Hunyadi
  • 1,770
  • 11
  • 26
  • `time_t` is 64-bits when it's supported. Not that current Epoch Time needs more than 32-bits to be represented, so if you are seeding it with time it makes no difference. – Havenard Jun 29 '17 at 07:31
  • See also: https://codereview.stackexchange.com/questions/109260/seed-stdmt19937-from-stdrandom-device – Paul R Jun 29 '17 at 07:35
  • You can do things like: auto duration = std::chrono::high_resolution_clock::duration{ std::chrono::high_resolution_clock().now().time_since_epoch() }; auto nanosec = std::chrono::duration_cast(duration).count(); auto seed = static_cast(nanosec); auto engine = std::mt19937_64{ seed }; – user515430 Jun 29 '17 at 16:34

0 Answers0