Lucas
Lucas

Reputation: 767

Why is rand_r not defined in std?

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

Answers (1)

Zeta
Zeta

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() and srand() conform to SVr4, 4.3BSD, C89, C99, POSIX.1-2001. The function rand_r() is from POSIX.1-2001. POSIX.1-2008 marks rand_r() as obsolete.

Even better, forget about rand and rand_r and use std::mt19937 with the correct distribution (see <random>).

Upvotes: 7

Related Questions