Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Srand() seed microsecs?

  1. Mar 31, 2009 #1

    Im using Visual basic c++ and was wonderig what the easiest way was to seed srand() with the time in microseconds is.

  2. jcsd
  3. Mar 31, 2009 #2


    User Avatar
    Science Advisor
    Homework Helper

    Which do you mean ?
    If you mean c++ you can seed srand() with QueryPerformanceCounter() it's the nearest you will get to a microsecond timer on windows.
  4. Mar 31, 2009 #3
    Cool that seems like a good plan, unfortunately, i don't know how to use QueryPerformanceCounter(), do you think you could explain it a little bit?

  5. May 6, 2009 #4

    Code (Text):
    #include <windows.h>

    int main()
      LARGE_INTEGER cicles;

      srand (cicles.QuadPart);

      return 0;
  6. May 6, 2009 #5
    There is no such thing as Visual basic c++. There is Visual Basic and there is C++, they are too completely different languages. There's also Visual Studio which contains the Visual C++ compiler.

    Also, there is almost no conceivable reason why you should want to seed the RNG with microsecond precision. If you're doing multiple experiments and you want random initial values, you should be doing them in the random sequence from 1 initial seed, not re-seeding it each time.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook