2

I usually seed my RNG by the following time, obtained by a call to time.h

#include <iostream>
#include <time.h>

using namespace std;



int main()
{
    cout << static_cast<unsigned int>(time(0)) << endl;
    return 0;
}

Is there an equivalent to static_cast<unsigned int>(time(0)) in the Boost-library?

BillyJean
  • 1,537
  • 1
  • 22
  • 39

1 Answers1

3

You can get the time using boost::posix_time. See this SO question. Ex:

boost::posix_time::time_duration diff = tick - now;
diff.total_milliseconds();

You can also use the C++11 chrono, if you can use C++11. Ex:

int elapsed_milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count();

With these two methods, you can get the number of milliseconds from the start of the day, and then assign it to your seed.

Community
  • 1
  • 1
Synxis
  • 9,236
  • 2
  • 42
  • 64