Last active
          June 6, 2016 17:54 
        
      - 
      
 - 
        
Save rbaulin/fb68a6812609f88934d1 to your computer and use it in GitHub Desktop.  
    random NSString in objective-c
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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