scbas1000 scbas1000 - 1 year ago 119
Swift Question

why drand48 () show same value in each run in my program?

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 ()

Answer Source

drand48 creates a sudo-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


let time = UInt32(NSDate().timeIntervalSinceReferenceDate)
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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download