I am using rand to get some "random" numbers, on Windows, Visual Studio 2010.
In the main file I've included and and using this:
srand(time(NULL))
And in another file, in the constructor of an object
(object is defined globally, due to its use in OpenGL functions(Init, Render)
I am calling rand() % bound, 10 times and getting the same result, both at my laptop and desktop.
This should be due to the object being created before the seeding of rand from main.
How can I avoid that and keep the object being global?
In the constructor of your global object, do srand(time(NULL))
before using rand
. Alternatively, have another non-local object with static storage duration that does srand(time(NULL))
. Make sure it is defined in the same translation unit but before your object that uses rand
. Non-local objects with static storage duration are initialised in definition order.