Part of what I’m developing is a random company name generator. It draws from several arrays of name parts. I use the rand() function to draw the random name parts. However, the same “random” numbers are always generated in the same sequence every time I launch the app, so the same names always appear.
So I searched around SO, and in C there is an srand() function to “seed” the random function with something like the current time to make it more random – like srand(time(NULL)). Is there something like that for Objective-C that I can use for iOS development?
The functions
rand()andsrand()are part of the Standard C Library and like the rest of the C library fully available for you to us in iOS development with Objective-C. Note that these routines have been superseded byrandom()andsrandom(), which have almost identically calling conventions torand()andsrand()but produce much better results with a larger period. There is also ansrandomdev()routine which initializes the state of the random number generator using the random number device. These are also part of the Standard C Library and available for use on iOS in Objective-C.