Reputation: 767
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?
Upvotes: 4
Views: 2218
Reputation: 105876
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>
).
Upvotes: 7