User login
Three Ways To Randomize on the iPhone
bosshogg — Wed, 11/25/2009 - 23:20
To the best of my knowledge, there are three ways to generate random numbers on the iPhone: The first, SecRandomCopyBytes:
UInt32 randomResult = 0;
int result = SecRandomCopyBytes(kSecRandomDefault, sizeof(int), (uint8_t*)&randomResult);
if (result != 0)
randomResult = arc4random();
The second, arc4random:
UInt32 randomResult = arc4random();
The last (and worst) rand():
UInt32 randomResult = rand();
I have found that these all work, however, arc4random and rand seem to generate numbers in a fairly "clumpy" fashion. I haven't mapped out runs of 1,000 or greater, but I believe that some of the complaints on earlier versions of our 10,000 game for the iPhone suffered from this clumpy nature. For example, the computer would roll 6 dice and without any time delay in generating the 6 numbers, the dice tended to come up as the same numbers, which in Farkle means lots of triples and quadruples which means lots of points. We had several people complain that the computer was cheating. After we did an app update and changed to SecRandomCopyBytes it didn't appear to happen as frequently.
Anyway, hopefully someone will find this useful and happy randomizing! Drop me a note if you have similar or differing experiences with randomizing on the iPhone: jon@threejacks.com
-Jon
- bosshogg's blog
- Login or register to post comments
Navigation
Recent blog posts
- How the iPad Killed Our iPhone App Sales
- Sending email from within iPhone apps
- iPhone Tie Down Calculator Created for Pacific Cargo Control
- My Opinings On The iPad
- Plug For A Good Small Company
- Gambling Planet Features Three Jacks Games
- Twist Tac Toads Available in the AppStore!
- Getting the Version String
- Your iPhone App Icon
- C++ On The iPhone
