Is this a good way to create a random string on an iPhone?

Here is the code I came up with:

NSString *randomString = @""; for (int x=0;x<NUMBER_OF_CHARS;x++) { randomString = [randomString stringByAppendingFormat:@"%c", (char)(65 + (arc4random() % 25))]; } return randomString; 

EDIT:

To respond to comments:

1) I am mainly interested in code multiplicity.

2) I also wonder if it is safe from guessing a string and / or proof against collisions if NUMBER_OF_CHARS is a large number (say 40) - not for this application, but in other cases.

3) Also, is there a faster way if I want to make a ton of lines once? It seems to be slow as it makes an object through a loop every time.

+7
source share
5 answers

If NUMBER_OF_CHARS is a compile-time constant, you can speed up your code by avoiding re-creating the object. You can make your program shorter by one line:

 char data[NUMBER_OF_CHARS]; for (int x=0;x<NUMBER_OF_CHARS;data[x++] = (char)('A' + (arc4random_uniform(26)))); return [[NSString alloc] initWithBytes:data length:NUMBER_OF_CHARS encoding:NSUTF8StringEncoding]; 

As far as I know, arc4random_uniform should be good enough for cryptographic applications, but you may need to contact a cryptography specialist if the data you plan to protect is of great value to you or especially to your customers.

EDIT: Edited in response to nielsbot comment.

+9
source

FWIW, I would prefer Vincent Gable’s suggestion to use UUID. If you are tuned to the proposed algorithms, you can get a little more variance using something like this nielsbot code variant (just replace the character string with what you want to include as part of your random strings) ...

 const NSUInteger NUMBER_OF_CHARS = 40 ; NSString * CreateRandomString() { static char const possibleChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; unichar characters[NUMBER_OF_CHARS]; for( int index=0; index < NUMBER_OF_CHARS; ++index ) { characters[ index ] = possibleChars[arc4random_uniform(sizeof(possibleChars)-1)]; } return [ NSString stringWithCharacters:characters length:NUMBER_OF_CHARS ] ; } 
+4
source

You can also use UUIDs , as they are pretty well studied and widely used . I would probably start there before trying to guarantee the likelihood of collisions in my manual solution.

+2
source

Here is a quick way to do it if you do it a lot.

 const NSUInteger NUMBER_OF_CHARS = 40 ; NSString * CreateRandomString() { unichar characters[NUMBER_OF_CHARS]; for( int index=0; index < NUMBER_OF_CHARS; ++index ) { characters[ index ] = 'A' + arc4random_uniform(26) ; } return [ NSString stringWithCharacters:characters length:NUMBER_OF_CHARS ] ; } 
0
source

Another option for existing answers. I post this because it (seems) to be more efficient in terms of performance, as it makes one call to the ARC4 API.

 NSString *random32CharacterString() { static const int N = 32; // must be even uint8_t buf[N/2]; char sbuf[N]; arc4random_buf(buf, N/2); for (int i = 0; i < N/2; i += 1) { sprintf (sbuf + (i*2), "%02X", buf[i]); } return [[NSString alloc] initWithBytes:sbuf length:N encoding:NSASCIIStringEncoding]; } 
0
source

All Articles