Generating Random Numbers in Objective C for iPhone SDK

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I was using the arc4random() function in order to generate a random group and sequence of numbers, but I was told that this was overkill and that I should use the random() function instead. However, the random() function gives me the same group and sequence of numbers every time.

I call srand(time(0)) once when my app first starts in order to seed the random() function. Do you ever need to reseed the random() function?

Am I missing something?

Thanks.

Answers

First off, who told you arc4random was overkill? I use it in my projects, and it (a) satisfies my requirements, (b) doesn t suck down resources (at least any visible to the user or obvious to me), and (c) was trivial to implement, so I don t really see how a similar use in your own code could be called "overkill."

Second, srand() seeds the rand() function, not random(), so that may be your issue. And no, you shouldn t have to reseed the generator at any time during your program s execution - once during startup is enough.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/1376869/generating-random-numbers-in-objective-c-for-iphone-sdk

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils