种子随机从毫秒在Windows和Linux [英] Seed random from milliseconds in Windows and Linux

查看:392
本文介绍了种子随机从毫秒在Windows和Linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要种子的随机数生成器boost(从一个int加载)几个进程,对于一个程序,必须在Windows和Linux中编译和工作。

I need to seed the random number generator in boost (which is loaded from an int) for a few processes, for a program that has to compile and work both in Windows and in Linux.

我使用 std:time(0),但是因为这些进程是同时运行的作业,其中一些将在同一秒钟运行,产生相同的结果。

I used std:time(0), which worked, but since the processes are jobs which are run simultaneously, some of them would run at the same second, producing the same results.

所以我需要种子从毫秒,或任何其他跨平台随机生成器物。我尝试了 clock()但不能使它从 time_t 转换为int。

So I need to seed it from milliseconds, or any other cross-platform random generator for that matter. I tried clock() but couldn't quite make it convert from time_t to int.

感谢。

推荐答案

如果您从单个脚本启动所有作业。

If you are starting all the jobs from a single script.

然后,您可以在命令行上传递一个递增的数字作为参数。每个作业然后将此值添加到time()的结果以生成其种子。

Then you could pass an incremented number as an argument on the command line. Each Job then adds this value to the result of time() to generate its seed.

注意:我没有看到OP中有关安全性的任何要求。 >
原始代码使用时间(NULL),这将有一个可猜的种子。

Note: I don't see any requirement in the OP about security.
The original code is using time(NULL) and this will has a guessable seed.

int main(int argc,char* argv[])
{
    srand(time(NULL) + boost::lexical_cast<int>(argv[1]));

    // STUFF
}

这篇关于种子随机从毫秒在Windows和Linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆