I am new to C++ and still trying to understand the nuances of the the syntax. In the below example i am using the srand() function to set my random seed. I am just wondering why srand needs to be in the main part of my program and cannot be outside in the front end of the program?
thank you for your time
Code:#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int seed_storage = time( NULL ); //set seed_storage to random seed value int randRange(int low, int high) { return rand()%(high+1-low)+low; //take the modulus of the generated number with respect to the difference between the high and low values of the range then add low. } int hiddenNumber; //value to hold random generated number int main() { srand(seed_storage); //use seed_storage value as seed for rand() function while(true) { hiddenNumber = randRange(0,100); } }



LinkBack URL
About LinkBacks



