For the purposes of an algorithm class that I am taking, we are asked to create an array of random numbers which will be used in a specified function. My question pertains to rand() and srand(). When should I use srand()? How often should it be used? Some of the material I've read online has suggested something of the sort:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <cstlib>
void getRandom () {
std::cout << "Random number: " << rand() << std::endl;
}
int main() {
srand(time(0)); // only call once in program?
getRandom();
return 0;
}
We are just using the random numbers to simulate possible user input. For example rand()&101 to represent possible grades on an exam.
The srand should be called at the beginning of the program (just after main), correct? I'm looking at creating a class in which the random function will a method of the class. It probably would not be a good idea to insert the srand() into the constructor of this object.