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

会有一股神秘感。 提交于 2019-12-13 08:58:18

问题


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



来源:https://stackoverflow.com/questions/39923706/why-drand48-show-same-value-in-each-run-in-my-program

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