So i've been building a simple console game in c++ where I am using the rand() function from stdlib.h. I have recognized that i got different results from the rand function if I was executing the program in debug mode or without debugging.
Here is my code where I use the rand function:
void Game::SelectHiddenWord(Fstring &word) {
std::vector<Fstring> WordList{ "plan","planet","planes","radios","gamers","images",
"amigos","micros","macros","frogs","raids","roads","paris","smog","cars","macs","scam","some","farm","fair",
"jam","rat","map","zig","zag","isogram","amorist","roaming","mirages","soaring","cargos","disarm","isobar"
};
int32 Length = WordList.size();
int32 randomNumber = std::rand()%Length;
word = WordList.at(randomNumber);
}
So basically I have a wordlist with isograms and I am using the rand function to generate an integer which I will use as my index when selecting a word from the wordlist. I found that every time I was executing the program the randomNumber integer was always 14 which would then give me the word "smog" from the wordlist. First I thought that this maybe was a bug since I've read that the rand() function isn't the best to use to generate random numbers. So I started the program in debug mode and I found out that I the randomNumber integer now was 32 which would give me the word "disarm" from the wordlist. And this happend every time I was using debugmode.
So on startup without debugging I always get the word "smog" and on startup with debugging I always get the word "disarm" so I can really just play around with two words.
So is this some sort of bug with the rand() function that it will behave differently when executing the program in debug mode and without debug mode?
I'm trying to learn c++ and I couldn't find any information about the rand function behaving differently so I do not know how I can fix this issue. So if any of you guys know how to fix this issue, so that the rand() function will behave the same and generate the same number independently if I am using debug mode or without debugging. Or maybe I need to use another function to generate my random numbers?
Any help will be appreciated!
Edit:
I do not want to know why the rand() function generates the same value everytime. When I start the program without debugging the rand() always generates the number 14, and I want this to happen. But if I start the program in debug mode the rand() always generates the number 32. But I want the rand function to always generate the number 14, independently if I execute the program in debug mode or without debugging.
So the question is about why the rand() function is behaving differently and generating different numbers if it is executed with or without debugging. I don't even know if it's possible to answer this, maybe it could just be something with my machine.
My testing code:
void Game::Print() {
Fstring HIDDEN_WORD{}; //This MUST be an isogram
SelectHiddenWord(HIDDEN_WORD);
MyHiddenWord= HIDDEN_WORD;
std::cout << MyHiddenWord << std::endl; //This prints "smog" to the screen if I start the program without debugging,
//and if I debug the program it prints "disarm" to the screen
My main method:
Game MyGame;
int main() {
MyGame.Print();
}
I am using Microsoft Visual C++ compiler.