Trying to seed srand() and time()

Hello,

I am trying to seed time() into srand(), in order to calculate a rand() number with a predefined parameter, in this way:

srand(time());

randomNum = 1 + rand() %10;

The above code should work, but it isn't the compiler (Visual Studio 2010) is complaining about he "srand(time());" part,

If anybody could help me make sense of this I wold greatly appreciated.

P.S. I did include the <ctime> header file idk if there is another header file to be included.
You need to include something inside the time function for it to work correctly. Either NULL or 0 is generally used for random numbers to generate a different number each time.

srand(time(NULL));
http://www.cplusplus.com/reference/clibrary/ctime/time/
but it isn't the compiler (Visual Studio 2010) is complaining about he "srand(time());" part,


For future reference, post the error message. The thing about error messages is they tend to tell you exactly what's wrong.


Anyway, time takes a parameter and you aren't giving it any:

 
srand(time(0));



EDIT: ninja'd

NULL and 0 are equivalent, assuming you have NULL defined (which you may not)
or if you have C++11 capabilities, you can use nullptr instead.
Last edited on
Topic archived. No new replies allowed.