How to generate a random number in iOS

We can use the C function rand() for this:

This generates an integer between 1 and 10. Alternatively you can use the arc4random function:

This will generate a random number between 0 and 9.

One thing of note is that often this function will generate the same number multiple times in a row which may not be what you want.

If you search the Xcode documentation you’ll also find a link to Apple’s Randomization Services. Read it and weep here:

About Jay Versluis

Jay is a medical miracle known as Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

2 thoughts on “How to generate a random number in iOS

Leave a Reply to iOSDev Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.