How do I seed a random generator and create a random int in Objective-C

I saw several examples of random int in Objective-C, but all people complain about the same sequence of numbers every time the application starts. I read about sowing a random number, but I'm not sure what that means.

How can an arbitrary number be generated differently every time, even after the application has rebooted?

Can some data be stored in NSUserDefaults, and then, depending on this, different values ​​are generated?

+3
source share
3 answers

Here's a discussion on the Apple Developer Forums .

arc4random() random() rand(). /dev/urandom . rand(), random() .

: man arc4random

#include <stdlib.h>
picknumber = arc4random() % 3 + 1; 
+8

:

srand([[NSDate date] timeIntervalSince1970]);

.

+5

I used arc4randomone that you do not need. You may try.

+1
source

All Articles