How do I generate a random lowercase letter in Objective-C?

objective-c, random

Solution

Here's a succinct way to do it:

- (char)getRandomChar {
    return (char) (arc4random_uniform(26) + 'a');
}

This assumes you want 'a' to 'z', without any letters that have diacritical marks, such as å, ä, á, etc.

To return the character as an NSString:

- (NSString *)getRandomCharAsNString {
    return [NSString stringWithFormat:@"%c", arc4random_uniform(26) + 'a'];
}

Problem

I'm new to the syntax, so that's where I need help. Conceptually, I get it. But syntax is foreign to me.

Original source