why drand48 () show same value in each run in my p

2019-09-21 09:14发布

问题:

I try to create a variable to get value random with drand48 () but in each run my variable take a same value . can anybody help me , (i want in each run my variable take different value) ???

let number = drand48 ()

回答1:

drand48 creates a Pseudo-random number sequence. That is if it is not set to a different start point each time it will always produce the same numbers.

To fix this call srand48 beforehand to set a new start point for drand48

Eg

let time = UInt32(NSDate().timeIntervalSinceReferenceDate)
srand48(Int(time))
let number = drand48 ()

Here are the docs for drand48 and srand48

Edit: different way to seed to avoid the error, I'm not by a computer (on my phone) so may have to fix it later if it doesn't work