Namespaces
Variants
Views
Actions

std::rand

From cppreference.com
 
 
 
 
Defined in header <cstdlib>
int rand();

Returns a uniformly distributed pseudo-random integral value between 0 and RAND_MAX (0 and RAND_MAX included).

srand() should be called before any calls to rand() to initialize the random number generator.

Contents

[edit] Parameters

(none)

[edit] Return value

Pseudo-random integral value between 0 and RAND_MAX.

[edit] Example

#include <cstdlib>
#include <iostream>
#include <ctime>
 
int main() 
{
    std::srand(time(0)); //use current time as seed for random generator
    int uniform_random_variable = std::rand();
    std::cout << "Uniform random value on [0 " << RAND_MAX << "]: " 
              << uniform_random_variable << '\n';
}

Possible output:

Uniform random value on [0 2147483647]: 1373858591

[edit] See also

initializes pseudo-random number generator
(function) [edit]