Skip to content

Instantly share code, notes, and snippets.

@rbaulin
Last active June 6, 2016 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbaulin/fb68a6812609f88934d1 to your computer and use it in GitHub Desktop.
Save rbaulin/fb68a6812609f88934d1 to your computer and use it in GitHub Desktop.
random NSString in objective-c
NSString *rand_str() {
NSString *alphabet = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789";
u_int32_t len = arc4random_uniform(20) + 5;
NSMutableString *s = [NSMutableString stringWithCapacity:len];
for (NSUInteger i = 0U; i < len; i++) {
u_int32_t r = arc4random() % [alphabet length];
unichar c = [alphabet characterAtIndex:r];
[s appendFormat:@"%C", c];
}
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment