-
srand() gayness
I'm initializing the seed in a for loop. How did i initialize "randomly" each time the for loop goes through an interation?
This is what i have now
Code:
for (i=1; i<5; i++)
{
srand((unsigned int) time(0));
}
What do i do to change the seed so that different psuedorandom numbers
are generated?
-
Why would you need to? :confused: Do it once and all your numbers should be (almost) perfectly randomized. :)
-
Are you seeing soem kind of weird behaviour? I don't see any problem with that code, although it seems a little pointless to seed it more than once. You should only have to seed it once.
-
I might be wrong, but I think VC++ gives you errors if you seed it more than once...I'll check when I get home from school...but for now I'm off for school. Bye :)
-
I just use this:
Code:
srand(GetTickCount());
Works every time :rolleyes:
-
Unless you're not using Windows of course ;)
-
Picky :rolleyes:
Code:
#include <time.h> /* or <ctime> in C++ */
// ...
void somecode() {
srand(time(NULL));
}
-
Or, just pick some number, and you will get the same random numbers everytime. This is kinda helpful if you have to, say, dynamically create something or other randomly. For example, srand(0), always gives the same random numbers, srand(1) gives another, etc, etc, etc.
Z.
-
I think he knew that already...