Asked 7 years ago
12 Feb 2017
Views 3145
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 Nov 30 '-1 00:00


    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
Post Answer