dimanche 5 avril 2020

How to generate a unique random number on the start of a program then get the number and call it in a function

I'm making a small game in c++ for fun and I make a random number between 0 and 100 which is the number of threats you need to solve, that works but I made a HomeScreen() function that would be called if the user wants to go back and see the threats or type in commands, well the problem is when I called my int for the threats, it created a new random number which messes up the whole game because it would erase or fastrack a users progress. I need to know how I can get the number of threats that were generated on the game's startup and get it and display it in a HomeScreen() function.

main.cpp (the first place where the int and random value is assigned)

int threats; srand(time(NULL)); threats = rand() % 100 + 1;

HomeScreen() Function
int threats;
srand(time(NULL));
threats = rand() % 100 + 1;
system("CLS");
std::cout << "| HackersBeGone | Home Page | " << std::endl;
std::cout << "Threats = " << threats << std::endl; // Here is displays another random number when I 
                                                   // need it to be the same number from main.cpp



Aucun commentaire:

Enregistrer un commentaire