Results 1 to 9 of 9

Thread: srand() gayness

  1. #1

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471

    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

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    Why would you need to? Do it once and all your numbers should be (almost) perfectly randomized.

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  4. #4
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    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

  5. #5
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    I just use this:
    Code:
    srand(GetTickCount());
    Works every time

  6. #6
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Unless you're not using Windows of course
    Harry.

    "From one thing, know ten thousand things."

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  8. #8
    Zaei
    Guest
    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.

  9. #9
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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
  •  



Click Here to Expand Forum to Full Width