I am writing a battleship game and I need two random numbers for the location of a ship. But I have two ships to I need to have two random numbers twice. Ofcourse those two pairs of random numbers cannot be the same.
The two functions are almost the same but have different integers. The second function has something extra because it cannot be the same as the first function.
My battleship games consists of a 2D array of 5x5.
The code is writen in Dutch but I translated the comments so you can understand the code.
The problem is that i get an error when i create a second pair.
You're calling srand() too often. It should be called just the once, at progam startup.
Every time you call srand() you are setting the seed. As you use time() for seeding, which is accurate to the second, if you call srand() again quickly enough it will be set to the same value which means that the number returned by the following calls to rand() will be identical.
Anyways; I believe you should refrain from using std::rand/std::srand. Afaik they're linear RNGs that use globals. Instead, why not use the newer, more C++ styled std::mt19937, in combination with the std::uniform_*_distribution.