rand() is a
pseudo (fake) random number generator, no matter how you seed it or set a range, especially if the range is small (1 - 10 is a small range), the beginning sequence of numbers can be predictably non-random.
From cppreference's rand page:
https://en.cppreference.com/w/cpp/numeric/random/rand
One workaround is to generate and discard a number of random numbers before using any generated number as being random.
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. |
C++11 introduced the <random> library, with several pseudo random engines and one true non-deterministic engine (
std::random_engine). Using the <random> library is really recommended, it is not as wonky as the C library
srand/rand functions.
Not every compiler properly implements
std::random_engine. GCC still might not, I haven't tested the newer versions. MinGW32 5.1.0 (latest Code::Blocks) doesn't.
I know Visual C++ does. VS2015 and VS2017.