Defined in header <cstdlib>  

int rand(); 
Returns a pseudorandom integral value between 0
and RAND_MAX
(0 and RAND_MAX
included).
std::srand()
seeds the pseudorandom number generator used by rand()
. If rand()
is used before any calls to srand()
, rand()
behaves as if it was seeded with srand(1)
.
Each time rand()
is seeded with srand()
, it must produce the same sequence of values on successive calls. Other functions in the standard library may call rand
, it is implementationdefined which functions do so.
It is implementationdefined whether rand()
is threadsafe.
(none).
Pseudorandom integral value between 0
and RAND_MAX
.
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 wellknown example, the loworder bit simply alternated between 1
and 0
between calls).
rand()
is not recommended for serious randomnumber generation needs, like cryptography. It is recommended to use C++11's random number generation facilities to replace rand(). (since C++11).
#include <cstdlib> #include <iostream> #include <ctime> int main() { std::srand(std::time(0)); // use current time as seed for random generator int random_variable = std::rand(); std::cout << "Random value on [0 " << RAND_MAX << "]: " << random_variable << '\n'; }
Possible output:
Random value on [0 2147483647]: 1373858591
(C++11)  produces integer values evenly distributed across a range (class template) 
seeds pseudorandom number generator (function) 

maximum possible value generated by std::rand (macro constant) 

generates a random integer in the specified range (function template) 

C documentation for rand 
© cppreference.com
Licensed under the Creative Commons AttributionShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/numeric/random/rand