I was wondering why I can't use std::rand_r
when including cstdlib
? Or, more generally, why are some functions in cstdlib
in the global namespace but not in the std
namespace?
rand_r
isn't part of the standard, it is a POSIX method.
rand_r(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
Conforming To
The functions
rand()
andsrand()
conform to SVr4, 4.3BSD, C89, C99, POSIX.1-2001. The functionrand_r()
is from POSIX.1-2001. POSIX.1-2008 marksrand_r()
as obsolete.
Even better, forget about rand
and rand_r
and use std::mt19937
with the correct distribution (see <random>
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With