推荐的方法来初始化srand? [英] Recommended way to initialize srand?

查看:125
本文介绍了推荐的方法来初始化srand?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个好的方式来初始化C ++中的伪随机数生成器。我找到一篇文章,说明:

I need a 'good' way to initialize the pseudo-random number generator in C++. I've found an article that states:


为了生成类似随机的
数字,srand通常将
初始化为一些独特的值,就像那些与执行时间相关的
。对于
示例,
函数时间(在标题
ctime中声明的)返回的值每秒不同,其中
对于大多数
随机需要是独特的。

In order to generate random-like numbers, srand is usually initialized to some distinctive value, like those related with the execution time. For example, the value returned by the function time (declared in header ctime) is different each second, which is distinctive enough for most randoming needs.

Unixtime对我的应用程序来说不够鲜明。什么是更好的方式来初始化这个?如果它是可移植的,但是代码将主要在Linux主机上运行。

Unixtime isn't distinctive enough for my application. What's a better way to initialize this? Bonus points if it's portable, but the code will primarily be running on Linux hosts.

我想要做一些pid / unixtime数学得到一个int,来自 / dev / urandom 的数据。

I was thinking of doing some pid/unixtime math to get an int, or possibly reading data from /dev/urandom.

谢谢!

EDIT

是的,我实际上每秒多次启动我的申请,我碰到碰撞。

Yes, I am actually starting my application multiple times a second and I've run into collisions.

推荐答案

最好的答案是使用Boost随机数字。或者如果您有权访问C ++ 11,请使用 < random> ;

The best answer is to use the Boost random number stuff. Or if you have access to C++11 use the <random> header.

但是如果我们在谈论 rand >和 srand()

最好的方法是使用 time()

But if we are talking about rand() and srand()
The best way is just to use time():

int main()
{
    srand(time(NULL));

    ...
}

在你的程序开始,而不是每次你调用 rand()

Be sure to do this at the beginning of your program, and not every time you call rand()!

,time()将返回一个唯一值(除非您每秒多次启动应用程序)。在32位系统中,它只会每60年左右重复一次。

Every time you start up, time() will return a unique value (unless you start the application multiple times a second). In 32 bit systems, it will only repeat every 60 years or so.

我知道你不认为时间是独一无二的,但我觉得很难相信。但我已经知道是错误的。

I know you don't think time is unique enough but I find that hard to believe. But I have been known to be wrong.

如果你同时启动了大量的应用程序的副本,你可以使用一个更精细的分辨率的定时器。但是,在该值重复之前,您会遇到较短时间段的风险。

If you are starting a lot of copies of your application simultaneously you could use a timer with a finer resolution. But then you run the risk of a shorter time period before the value repeats.

确定,因此如果您确实认为您每秒钟启动多个应用程序。

然后在计时器上使用更细粒度。

OK, so if you really think you are starting multiple applications a second.
Then use a finer grain on the timer.

 int main()
 {
     struct timeval time; 
     gettimeofday(&time,NULL);

     // microsecond has 1 000 000
     // Assuming you did not need quite that accuracy
     // Also do not assume the system clock has that accuracy.
     srand((time.tv_sec * 1000) + (time.tv_usec / 1000));

     // The trouble here is that the seed will repeat every
     // 24 days or so.

     // If you use 100 (rather than 1000) the seed repeats every 248 days.

     // Do not make the MISTAKE of using just the tv_usec
     // This will mean your seed repeats every second.
 }

这篇关于推荐的方法来初始化srand?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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