This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of CD1 status.

395. inconsistencies in the definitions of rand() and random_shuffle()

Section: 29.9 [c.math] Status: CD1 Submitter: James Kanze Opened: 2003-01-03 Last modified: 2017-02-03

Priority: Not Prioritized

View all other issues in [c.math].

View all issues with CD1 status.


In 29.9 [c.math], the C++ standard refers to the C standard for the definition of rand(); in the C standard, it is written that "The implementation shall behave as if no library function calls the rand function."

In 28.6.13 [alg.random.shuffle], there is no specification as to how the two parameter version of the function generates its random value. I believe that all current implementations in fact call rand() (in contradiction with the requirement avove); if an implementation does not call rand(), there is the question of how whatever random generator it does use is seeded. Something is missing.

Proposed resolution:

In [lib.c.math], add a paragraph specifying that the C definition of rand shal be modified to say that "Unless otherwise specified, the implementation shall behave as if no library function calls the rand function."

In [lib.alg.random.shuffle], add a sentence to the effect that "In the two argument form of the function, the underlying source of random numbers is implementation defined. [Note: in particular, an implementation is permitted to use rand.]


The original proposed resolution proposed requiring the two-argument from of random_shuffle to use rand. We don't want to do that, because some existing implementations already use something else: gcc uses lrand48, for example. Using rand presents a problem if the number of elements in the sequence is greater than RAND_MAX.