srand(time(null)) on iPhone don't work =(

吃可爱长大的小学妹 提交于 2019-12-02 18:15:29

问题


I need generate random numbers in my iPhone game. I using rand() function. But it isn't enough random. I tried using srand with time(Null). But my random generator was periodic. /dev/random isn't an answer because I need new random number each 0.1 sec.


回答1:


On iOS and OS X, use arc4random instead. Higher quality randomness, and no need to worry about seeding.

However, rand() shouldn't be noticably periodic, unless you're calling srand each time around. Or using the random numbers incorrectly.




回答2:


You could try random() / srandom() instead. Better generator than rand() / srand().




回答3:


Are you building for debug or on the simulator? I know I ran into this kind of issue when building some windows applications in debug, the RNG was automatically seeded with a constant value to produce consistent results. You may want to check what it is actually seeded with, and if you are getting different outputs at all each run.



来源:https://stackoverflow.com/questions/5912799/srandtimenull-on-iphone-dont-work

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