First, the srand() and rand() are functions from C Standard Library. They have known issues. C++ Standard Library has much better tools for "random".
George P shows how to use them. Learn the C++ tools.
The rand() has a known serie of values. Each call of rand() returns the next value from that serie.
The srand() resets rand() to specific point in the serie, sets where to read the next value from.
In a quick loop the time does not change between calls of srand(). Therefore, every call of rand() reads the same value from the serie.
When you debug the program, which can slow down execution, the time can advance between calls to srand() and therefore each rand() will return a value from different part of the serie. Different value.
You essentially had:
1 2 3 4 5 6 7 8
|
std::vector<int> vec{};
for ( int i{}; i < 5; i++) {
vec.emplace_back( 16 );
}
for ( const auto i : vec ) {
std::cout << i << " ";
}
|
And assumed due to output that the loop did same as:
1 2 3
|
for ( const auto i : vec ) {
std::cout << vec[0] << " ";
}
|
Lets suppose that you had generated random numbers, but that serie would have been:
2032415, 15, 715, 672315, 3415
These too would have produced the 16, 16, 16, 16, 16.
(It is quite unlikely that anywhere in the rand() serie are five consecutive *15, but not impossible.)