Asked 30+ days ago
12 Feb 2017
Views 358
jaman

jaman posted

Generate a random alphanumeric string in Objective c at iOS

how to generate random alphanumeric string in objective c . working with iOS app .

library or any peace of code , which help to create random alphanumeric string in objective c
Mitul Dabhi

Mitul Dabhi
answered Jan 23 '18 20:17


    NSUInteger len =12;
    NSString *letterd =@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789#&@!?";
    NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
    
     for (NSUInteger i = 0; i < len; i++) {
        u_int32_t r = arc4random() % [letterd length];
        unichar c = [letterd characterAtIndex:r];
        [randomString appendFormat:@"%C", c];
    }
    NSLog(randomString);

One can change len to change random string length
arc4random() is hero of this code which generate random number , The arc4random() function returns pseudo-random pseudorandom
random numbers in the range of 0 to (2**32)-1
Edit Answer
Share
Post Answer