Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one seed the random number generator in Swift?

Tags:

random

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?

EDIT:

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

like image 355
quemeful Avatar asked Sep 17 '14 15:09

quemeful


1 Answers

This works:

let time = UInt32(NSDate().timeIntervalSinceReferenceDate)
srand(time)
print("Random number: \(rand()%10)")
like image 134
Alberto Barrera Avatar answered Sep 24 '22 18:09

Alberto Barrera