How to create an NSString with one random letter?
cocoa, nsstring
Solution
You can just make an NSString containing what you consider to be letters and pull a random character from it. Here's an example category:
@implementation NSString(RandomLetter)
- (NSString *)randomLetter {
return [self substringWithRange:[self rangeOfComposedCharacterSequenceAtIndex:random()%[self length]]];
}
@end
(You'll need to `srandom(time())` at some point, obviously. Maybe include an initialize method in your category.)
Problem
I need to create an NSString that has a single random uppercase letter. I can get random int's fine, and I could construct a C string from it and then make the NSString, but I imagine there has to be a better and more cocoa-ish way. Thanks!