0

Is there a way to simply seed a random generator but by using the <random> header instead of rand() and srand()?

I found the documentation about it quite abstruse.

I did notice that there is a seed_sq entry but I fail to understand it and it lacks example.

Lastly, my search engine only yielded me results on SO about srand and rand.

Please bear in mind that I'm "new" with C++ and generating random numbers in this language seems more complicated than, let's say, in Python.

jeromej
  • 10,508
  • 2
  • 43
  • 62
  • 1
    Some folk find "abstruse" abstruse:-) Would you mind being more specific? Which random generator are you wanting to use? – Bathsheba Jul 09 '15 at 07:11
  • 2
    Do you mean something like `std::random_device rd; std::mt19937 gen(rd());`? – Zeta Jul 09 '15 at 07:12
  • @Bathsheba Indeed. Well I'm not sure, I'm _"new"_ to C++ and read that `srand` and `rand` aren't ideal to generate proper random numbers. Even more about getting a number from a range, which is, I found, quite complicated to do properly when not using the `random` header (skewed results, …) – jeromej Jul 09 '15 at 07:14
  • Your best bet, in the absence of any other information, is to use the Mersenne Twister generator: part of C++11. But it *critically* depends on your use case. – Bathsheba Jul 09 '15 at 07:15
  • 1
    @JeromeJ: Well, you need to seed your random engine with _something_. That could be the number of seconds since 1970 (`time()` or `chrono`), a constant number (like `42`) or a non-deterministic number given by your OS/hardware (`random_device`). – Zeta Jul 09 '15 at 07:19
  • @Zeta Oh? Is it what `gen(rd())` does? If so, now I understand. I only seen this code sample without explanation. If it is, do not hesitate to post it as an answer. – jeromej Jul 09 '15 at 07:21
  • 1
    For MANY cases, using `srand` and `rand` is perfectly adequate. The major downfall is the fact that there is no specification for what RAND_MAX is, or how long the sequence is before it repeats, etc. Sure, mt19937 is probably a "better" random number generator in that it has a long time before it repeats... But there is nothing saying that `rand` can't be a MT19937 implementation! If you just want something to simulate throwing a dice and you're not losing $$$$ if someone can predict the sequence, `rand` is probably good enough. – Mats Petersson Jul 09 '15 at 07:35
  • @MatsPetersson Very nice to know! I just thought that the C++11 way to do it properly ( http://stackoverflow.com/a/20136256/ ) was simpler than the `rand` one ( http://stackoverflow.com/a/2999130/ ) but then I wondered how to seed the `random` version. I think @Zeta answered my question but I'm waiting for confirmation of my guess. – jeromej Jul 09 '15 at 07:39
  • @JeromeJ , here is an example for `seed_seq` and random number generation: http://stackoverflow.com/a/31090804/1915854 – Serge Rogatch Jul 09 '15 at 07:59
  • The example on this page shows how to seed random number generation only using C++11 stuff... http://www.cplusplus.com/reference/random/uniform_int_distribution/operator()/ But `srand()` and `rand()` are probably fine. They're certainly easier than using the new methods. – QuestionC Jul 09 '15 at 09:22

0 Answers0