How to sow rand () function in Objective-C?

Part of what I am developing is a random company name generator. It relies on several arrays of name parts. I use a function rand()to draw random parts of a name. However, the same "random" numbers are always generated in the same sequence every time I launch the application, so the same names always appear.

So, I was looking around of SO, and in C is a function srand(), to "seed" the random function with something like the current time, to make it more random - eg srand(time(NULL)). Is there something similar for Objective-C that I can use for iOS development?

+5
source share
2 answers

The functions rand()and srand()are part of the C standard library and, like the rest of the C library, fully accessible to you in iOS development using Objective-C. Note that these routines have been replaced with random()and srandom(), which almost identically cause agreements with rand()and srand(), but give much better results with a longer period. There is also a procedure srandomdev()that initializes the state of a random number generator using a random number device. They are also part of the C standard library and are available for use on iOS in Objective-C.

+8
source

Why don't you use arc4randomone that does not require seeds? You use it as follows:

int r = arc4random();

, rand(). arc4random() rand():

arc4random() , arc4, 8 * 8 8       S-Box. S- (2 1700). arc4random() -       0 (2 32) -1 , , rand (3)       (3).

, arc4random_uniform(). , 0 10, :

int i = arc4random_uniform(11);

man-:

arc4random_uniform (upper_bound) , upper_bound. arc4random_uniform() , `` arc4random()% upper_bound '', " ", .

+29

All Articles