0

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 ()
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
scbas1000
  • 121
  • 1
  • 12

1 Answers1

10

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

Olivier Wilkinson
  • 2,836
  • 15
  • 17