|
-
May 21st, 2001, 06:47 PM
#1
Thread Starter
Hyperactive Member
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?
Bababooey
Tatatoothy
Mamamonkey
-
May 21st, 2001, 09:36 PM
#2
Frenzied Member
Why would you need to? Do it once and all your numbers should be (almost) perfectly randomized.
-
May 22nd, 2001, 03:35 AM
#3
Frenzied Member
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.
Harry.
"From one thing, know ten thousand things."
-
May 22nd, 2001, 06:03 AM
#4
Frenzied Member
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
-
May 22nd, 2001, 08:28 AM
#5
Guru
I just use this:
Code:
srand(GetTickCount());
Works every time
-
May 22nd, 2001, 11:05 AM
#6
Frenzied Member
Unless you're not using Windows of course
Harry.
"From one thing, know ten thousand things."
-
May 22nd, 2001, 12:21 PM
#7
Monday Morning Lunatic
Picky 
Code:
#include <time.h> /* or <ctime> in C++ */
// ...
void somecode() {
srand(time(NULL));
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 23rd, 2001, 11:57 AM
#8
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.
-
May 23rd, 2001, 12:03 PM
#9
Frenzied Member
I think he knew that already...
Harry.
"From one thing, know ten thousand things."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|