The short answer
No. rand() produces pseudo-random numbers. Each value is calculated from the internal state left by the last one, so the whole sequence is set by the starting seed. The numbers look random for many purposes, but they are not truly random. The C and C++ standards also leave the method up to each implementation, so quality varies from one system to another.
What pseudo-random means
A pseudo-random generator keeps a small internal state. Each call to rand() updates that state and returns a number based on it. Nothing new comes in from the outside world, so if you know the state, you know every value that follows.
Run this program twice on the same system and you should see the same three numbers both times:
#include <cstdlib>
#include <iostream>
int main() {
std::srand(7);
for (int i = 0; i < 3; ++i) {
std::cout << std::rand() << '\n';
}
}
Why the same seed gives the same sequence
Seeding sets the internal state. Calling srand(7) puts the generator back at the same starting point each time, so the sequence starts over. Programs often seed with the current time, because it changes from second to second and each run then starts in a different place. The time is still predictable and changes only once a second, so two runs started in the same second get the same numbers.
If srand is never called, the generator starts from the same default seed as srand(1).
Does the sequence ever repeat?
Every pseudo-random generator has a finite state, so its sequence eventually repeats. The length before that happens is called the period. The C standard does not promise a period for rand(), and it varies by implementation. The engines in <random> are fully specified. For example, std::mt19937 has a period of 2^19937 - 1, far longer than any program will use.
How random does it need to be?
For a game, a shuffle or a simple simulation, the output of rand() is often good enough, though quality varies by system. It is a poor fit where predicting the numbers would cause harm, such as with passwords, encryption keys or session tokens.
Even std::mt19937, which has much better statistical properties, is not cryptographically secure. Enough observed outputs can reveal its internal state, and then its later values can be predicted.
Why quality varies
The C standard only says that rand() returns integers from 0 to RAND_MAX. It does not require a particular algorithm. Simple generators are known to have weak low-order bits, so using rand() % 2 as a coin flip is a poor choice on some systems.
Better options in C++
For new code, the <random> header is the better choice:
#include <iostream>
#include <random>
int main() {
std::mt19937 gen(std::random_device{}());
std::uniform_int_distribution<int> coin(0, 1);
std::cout << coin(gen) << '\n';
}
The engine here is std::mt19937, a Mersenne Twister, and the distribution gives each value an equal chance. For the full set of options, see how to generate random numbers in c.
Summary
rand()is pseudo-random, not truly random.- A given seed produces the same sequence on the same system.
- Quality varies by system, and
rand()is not a security tool. - For games and simulations,
<random>is the better choice. For secrets, use a cryptographic API.