Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/random/rand"

From cppreference.com
< cpp‎ | numeric‎ | random
m (headers sorted, fmt)
m (Example: + ",")
Line 39: Line 39:
 
     std::srand(std::time(nullptr)); // use current time as seed for random generator
 
     std::srand(std::time(nullptr)); // use current time as seed for random generator
 
     int random_variable = std::rand();
 
     int random_variable = std::rand();
     std::cout << "Random value on [0 " << RAND_MAX << "]: "  
+
     std::cout << "Random value on [0, " << RAND_MAX << "]: "  
 
               << random_variable << '\n';
 
               << random_variable << '\n';
  
Line 53: Line 53:
 
|p=true
 
|p=true
 
|output=
 
|output=
Random value on [0 2147483647]: 726295113
+
Random value on [0, 2147483647]: 726295113
 
6 3 6 2 6 5 6 3 1 1 1 6 6 6 4 1 3 6 4 2
 
6 3 6 2 6 5 6 3 1 1 1 6 6 6 4 1 3 6 4 2
 
}}
 
}}

Revision as of 10:00, 5 May 2023

 
 
 
 
Defined in header <cstdlib>
int rand();

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

std::srand() seeds the pseudo-random number generator used by rand(). If rand() is used before any calls to std::srand(), rand() behaves as if it was seeded with std::srand(1).

Each time rand() is seeded with std::srand(), it must produce the same sequence of values on successive calls.

Other functions in the standard library may call rand. It is implementation-defined which functions do so.

It is implementation-defined whether rand() is thread-safe.

Contents

Parameters

(none)

Return value

Pseudo-random integral value between 0 and RAND_MAX.

Notes

There are no guarantees as to the quality of the random sequence produced. In the past, some implementations of rand() have had serious shortcomings in the randomness, distribution and period of the sequence produced (in one well-known example, the low-order bit simply alternated between 1 and 0 between calls).

rand() is not recommended for serious random-number generation needs. It is recommended to use C++11's random number generation facilities to replace rand().(since C++11)

Example

#include <cstdlib>
#include <ctime>
#include <iostream>
 
int main() 
{
    std::srand(std::time(nullptr)); // use current time as seed for random generator
    int random_variable = std::rand();
    std::cout << "Random value on [0, " << RAND_MAX << "]: " 
              << random_variable << '\n';
 
    // roll 6-sided dice 20 times
    for (int n = 0; n != 20; ++n)
    {
        int x = 7;
        while (x > 6) 
            x = 1 + std::rand() / ((RAND_MAX + 1u) / 6); // Note: 1 + rand() % 6 is biased
        std::cout << x << ' ';
    }
}

Possible output:

Random value on [0, 2147483647]: 726295113
6 3 6 2 6 5 6 3 1 1 1 6 6 6 4 1 3 6 4 2

See also

produces integer values evenly distributed across a range
(class template) [edit]
seeds pseudo-random number generator
(function) [edit]
maximum possible value generated by std::rand
(macro constant) [edit]
generates a random integer in the specified range
(function template) [edit]