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

  • Context: C/C++ 
  • Thread starter Thread starter pazmush
  • Start date Start date
  • Tags Tags
    Seed
Click For Summary

Discussion Overview

The discussion revolves around the method of seeding the `srand()` function with time in microseconds while using Visual Basic C++. Participants explore different approaches and clarify terminology related to the programming languages involved.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant inquires about the easiest way to seed `srand()` with time in microseconds in Visual Basic C++.
  • Another participant suggests using `QueryPerformanceCounter()` as a method to achieve microsecond timing on Windows.
  • A subsequent reply requests clarification on how to use `QueryPerformanceCounter()` for seeding.
  • A code snippet is provided demonstrating the use of `QueryPerformanceCounter()` to seed `srand()`.
  • One participant challenges the terminology, stating that Visual Basic and C++ are distinct languages and questions the necessity of seeding the RNG with microsecond precision.
  • This participant argues that using a single initial seed for multiple experiments is preferable to re-seeding each time.

Areas of Agreement / Disagreement

Participants express differing views on the terminology of Visual Basic C++ and the necessity of microsecond precision for seeding the RNG. No consensus is reached regarding the best approach to seeding.

Contextual Notes

There is a lack of clarity regarding the definitions of Visual Basic and C++, as well as the implications of using microsecond precision for random number generation. The discussion does not resolve these issues.

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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 0 ·
Replies
0
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
4
Views
1K
Replies
4
Views
4K
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
19
Views
2K