c++ - 为 Windows 提供带有时间函数的 srand()

标签 c++ time visual-studio-2012 gettimeofday

我一直在寻找下面代码补丁的确切 Win 实现。我不是一个伟大的程序员,我的代码最初是在 UNix 中,但我必须尽可能少地修改它以用于 Win。我从一些谷歌搜索中了解到,主要问题是“gettimeofday”,它没有 Win 等价物。但是,我希望尽可能保留相同的程序结构。

struct timeval tv;
time_t curtime;
gettimeofday(&tv, NULL);    // ???
curtime = tv.tv_usec;
srand(tv.tv_usec); //time(&curtime)

最佳答案

如果您需要随机种子作为亚秒级数字,请执行以下操作:

#ifdef WIN32
::srand( GetTickCount() );
#else
//your existing code
#endif

关于c++ - 为 Windows 提供带有时间函数的 srand(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12390900/

相关文章:

c# - 为什么空委托(delegate)事件处理程序会导致 CA1061 警告?

c++ - 保存数组地址的指针地址如何相同?

c++ - 将字符串(包含数字)转换为整数并返回该整数

javascript - 雅虎财经 xchange - 获取最后更新时间

javascript - 在计时器上显示当前视频时间(小时 :minutes:seconds)

将 C 代码编译成与 WinXP 和 Win7 兼容的单个可执行文件

c++ - 功能的条件评估

c++ - 错误 : cast to incomplete array type "int []" is not allowed

Android - 获取计时器小部件的时间

visual-studio-2012 - SSDT BI for Visual Studio 2013 是否可以与 SQL Server 2012 一起使用?