Generate Random Numbers with Fixed Digits Length?

Amir iDev picture Amir iDev · Sep 27, 2013 · Viewed 8.7k times · Source

I am generating Random Number with

int randomID = arc4random() % 3000;

But I want to generate random number with atleast 4 digits. like 1000, 2400, 1122

I want to know the code for Objective C.

Answer

lanbo picture lanbo · Sep 27, 2013

Please try

generate numbers :1000~9999

int randomID = arc4random() % 9000 + 1000;

generate numbers :1000~2999

int randomID = arc4random() % 2000 + 1000;