In c++ the rand() is generating different result when executing the program with and without debugging

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



In c++ the rand() is generating different result when executing the program with and without debugging



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.





You should seed your random number generator 1 time in at the begining of main().
– drescherjm
23 mins ago






I am not sure why the release and debug rand() result is different.
– drescherjm
22 mins ago





@πάντα ῥεῖ: I think the dup does not answer the question; actually I'd expect that debug and release behave the same, even if srand is not called explicitly: "std::srand() seeds the pseudo-random number generator used by rand(). If rand() is used before any calls to srand(), rand() behaves as if it was seeded with srand(1). Each time rand() is seeded with srand(), it must produce the same sequence of values on successive calls."
– Stephan Lechner
15 mins ago






@StephanLechner What question after all?
– πάντα ῥεῖ
5 mins ago





@StephanLechner you are very correct. Should we reopen it and then close it again as off topic, questions that seek debug help should contain mcve?
– Öö Tiib
4 mins ago









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard