Start here · Essential guide
How to Generate Random Numbers in C++
Generate random numbers in C++ with the random header, std::mt19937 and distributions. Examples cover ranges, decimals and shuffling, plus the older rand().
Strings, Containers and Memory
How to generate random numbers in C++ and the limits of rand and srand.
Start here · Essential guide
Generate random numbers in C++ with the random header, std::mt19937 and distributions. Examples cover ranges, decimals and shuffling, plus the older rand().
Many programs use a pseudo-random generator with a seed, while secure values come from the operating system. Here is how that choice works, with a C++ example.
rand() returns an int from 0 to RAND_MAX. Learn how to call it, limit the range, get decimals and build a dice roller, and see its main limits.
Seed rand() with srand() once, at the start of your program, using the current time or a fixed value. Covers the C and C++ code and the loop mistake to avoid.
No. rand() is pseudo-random: each number follows from the state before it, so a given seed gives the same sequence. Here is what that means for code.
Related topics