Objective-c – Generating Random Numbers in Objective C for iPhone SDK

iphonenumbersobjective-crandom

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.

Best Solution

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.