如何在Swift中为随机数生成器播种? [英] How does one seed the random number generator in Swift?
本文介绍了如何在Swift中为随机数生成器播种?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用使用随机数.我想播种随机数生成器,以便每次都不相同.我该怎么做呢?
My app uses random numbers. I would like to seed the random number generator so that it won't be the same every time. How might I go about doing this?
我应该给srand()哪个参数来为当前时间的随机生成器提供种子?
What parameter do I give srand() to seed the random generator with the current time?
推荐答案
这有效:
let time = UInt32(NSDate().timeIntervalSinceReferenceDate)
srand(time)
print("Random number: \(rand()%10)")
这篇关于如何在Swift中为随机数生成器播种?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文