C/C++ How can I seed srand() with time in microseconds in Visual Basic/C++?

  • Thread starter Thread starter pazmush
  • Start date Start date
  • Tags Tags
    Seed
AI Thread Summary
The discussion centers on seeding the `srand()` function in Visual Basic C++ with time in microseconds. A participant suggests using `QueryPerformanceCounter()` for this purpose, as it provides high-resolution timing on Windows. An example code snippet is provided to demonstrate its implementation. However, there is a clarification that Visual Basic and C++ are distinct languages, and the term "Visual Basic C++" is incorrect. Additionally, it is noted that seeding the random number generator (RNG) with microsecond precision may not be necessary, as using a single seed for multiple experiments can yield better randomness.
pazmush
Messages
32
Reaction score
0
Hi

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

Thanks
 
Technology news on Phys.org
pazmush said:
Im using Visual basic c++
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.
 
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?

Thanks
 
Easy:

Code:
#include <windows.h>

int main()
{
  LARGE_INTEGER cicles;

  QueryPerformanceCounter(&cicles);
  srand (cicles.QuadPart);

  return 0;
}
 
pazmush said:
Hi

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

Thanks

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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top