Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is rand_r not defined in std?

Tags:

c++

random

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?

like image 912
Lucas Avatar asked Sep 15 '25 06:09

Lucas


1 Answers

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>).

like image 129
Zeta Avatar answered Sep 16 '25 21:09

Zeta