W3cubDocs

/C++

RAND_MAX

Defined in header <cstdlib>
#define RAND_MAX /*implementation defined*/

Expands to an integer constant expression equal to the maximum value returned by the function std::rand. This value is implementation dependent. It's guaranteed that this value is at least 32767.

Example

#include <climits>
#include <cstdlib>
#include <ctime>
#include <iostream>
 
int main()
{
    // use current time as seed for random generator
    std::srand(std::time(NULL));
 
    std::cout << "RAND_MAX: " << RAND_MAX << '\n'
              << "INT_MAX: " << INT_MAX << '\n'
              << "Random value on [0,1]: "
              << static_cast<double>(std::rand()) / RAND_MAX << '\n';
}

Possible output:

RAND_MAX: 2147483647
INT_MAX: 2147483647
Random value on [0,1]: 0.618608

See also

generates a pseudo-random number
(function)
seeds pseudo-random number generator
(function)
C documentation for RAND_MAX

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/numeric/random/RAND_MAX