0

I don't know if this question makes sense, but for example, python's random module has the Mersenne Twister 19937 as the PRNG behind it.

What would be the equivalent for C++'s random function?

edit: I know not to use the rand() function, I'm just running some test and was wondering if there was such a thing, or if it just is a LCG.

  • 5
    While someone will come along and actually provide an answer or a link to an answer, I'm just here to say: don't use `rand()`. Get to know and prefer the functionality available in ``. – sweenish Oct 18 '22 at 14:37
  • 4
    _"C++'s random function"_ - C++ has a few and [Mersenne Twister](https://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine) is one of them. Don't use `rand()` since you don't know what you'll get on different platforms. – Ted Lyngmo Oct 18 '22 at 14:38
  • Use the functions in the [random](https://en.cppreference.com/w/cpp/numeric/random) header. Don't use `rand` - you don't know what you'll get and it can be as bad as a linear congruential generator. – Jesper Juhl Oct 18 '22 at 14:41
  • 1
    C++'s equivalent of the python module you mentioned, would be using `std::mt19937`. See: https://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine – wohlstad Oct 18 '22 at 14:42
  • "*I know not to use the rand() function*" One of the reasons pointed out in the linked answer answers your question. Specifically, there is no answer because there is no required algorithm. Different implementations use different algorithms. – Nicol Bolas Oct 18 '22 at 14:50
  • @wohlstad Appreciate your comment, but my question is the other way around, is there any specific PRNG behind that specific function? – Miguel F. Serna Oct 18 '22 at 14:50
  • @NicolBolas Oops I missed that, I appreciate it. – Miguel F. Serna Oct 18 '22 at 14:52
  • i would expect all versions of the same compiler to use the same implementation, so the same code works the same across compiler versions. and it probably an LCG - but no guarantees – Neil Butterworth Oct 18 '22 at 14:56
  • [Obligatory XKCD that's depressingly close to accurate](https://xkcd.com/221/). – user4581301 Oct 18 '22 at 15:23
  • Obligatory link to [`rand` Considered Harmful](https://learn.microsoft.com/en-us/events/goingnative-2013/rand-considered-harmful) – user4581301 Oct 18 '22 at 15:28

0 Answers0