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

  • Thread starter pazmush
  • Start date
  • Tags
    Seed
In summary, the easiest way to seed srand() with the time in microseconds is to use QueryPerformanceCounter() in Windows. This can be done by including <windows.h> and using the LARGE_INTEGER data type. However, it is not recommended to seed with microsecond precision for random initial values.
  • #1
pazmush
32
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
  • #2
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.
 
  • #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?

Thanks
 
  • #4
Easy:

Code:
#include <windows.h>

int main()
{
  LARGE_INTEGER cicles;

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

  return 0;
}
 
  • #5
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.
 

1. What is the purpose of using srand() seed microsecs?

The srand() function is used to initialize the random number generator in C++. The seed microsecs parameter is used to set the starting point for generating random numbers. This ensures that the sequence of random numbers generated is different each time the program is run.

2. How is the seed microsecs calculated in srand()?

The seed microsecs is typically calculated using the current time in microseconds. This ensures that the seed is different for each run of the program, creating a new sequence of random numbers.

3. Can I use any value for the seed microsecs in srand()?

It is recommended to use a value that is unique for each run of the program, such as the current time in microseconds. Using the same value for the seed microsecs can result in the same sequence of random numbers being generated.

4. How does srand() seed microsecs affect the distribution of random numbers?

The seed microsecs does not directly affect the distribution of random numbers. However, if the same seed is used, the same sequence of numbers will be generated, which can affect the distribution if the program relies on a specific range of numbers.

5. Are srand() seed microsecs necessary for every program?

No, srand() seed microsecs are not necessary for every program. They are primarily used in programs that require a different sequence of random numbers each time they are run, such as in simulations or games. Programs that do not require random numbers can omit the use of srand() altogether.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
256
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
0
Views
217
  • Programming and Computer Science
Replies
1
Views
235
  • Programming and Computer Science
Replies
1
Views
628
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
19
Views
2K
Back
Top