How does one seed the random number generator in Swift?

大兔子大兔子 提交于 2020-01-11 02:57:20

问题


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?

EDIT:

What parameter do I give srand() to seed the random generator with the current time?


回答1:


This works:

let time = UInt32(NSDate().timeIntervalSinceReferenceDate)
srand(time)
print("Random number: \(rand()%10)")


来源:https://stackoverflow.com/questions/25895081/how-does-one-seed-the-random-number-generator-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!