标签: c++
当使用time来初始化srand时,我应该将time_t的time返回值显式地转换为unsigned int,还是直接将它传递给srand?
即我应该这样做: srand(static_cast<unsigned int>(time(0))) 要不就 srand(time(0))?
srand(static_cast<unsigned int>(time(0)))
srand(time(0))
为什么呢?
答案 0 :(得分:3)
time_t是typedef。由于srand采用unsigned int参数,并且任何整数类型都可隐式转换为unsigned int,因此无论是否使用显式static_cast<unsigned int>,您都会获得完全相同的行为。
time_t
typedef
srand
unsigned int
static_cast<unsigned int>
如果你有c ++ 11可用,我强烈建议使用<random>标题中提供的工具来生成任何严重的随机数。为了对此进行彻底的讨论,我建议watching Walter Brown在2016年CppCon上的演讲。
<random>