0

I'm trying to create a method that creates a random string consisting of 32 characters. This method will generate a random number using arc4random_uniform(62) to choose a number between 0 and 61 and then chose a character from a string that holds numbers from 0 to 9 and alphabet letters both small and capital letters, respectively. For an instance, if arc4random_uniform(62) returns 10, the chosen character will be a, if it returns 61, the chosen character will be Z). The method will do this for 32 times to create the final generated string.

I was wondering when this approach will fail to generate a unique String and result in a repeated one. I searched about this topic and didn't find a satisfying answer. I hope that you will help with me this since I am trying to use this method to generate unique IDs for use in my app.

ahmdx
  • 881
  • 7
  • 13
  • You can use [CFUUID][1] to create unique id. [1]: http://stackoverflow.com/questions/427180/how-to-create-a-guid-uuid-using-the-iphone-sdk/9981531#9981531 – Thilo Jun 08 '14 at 23:27
  • @Thilo, I was thinking of way that would work on other platforms as well. – ahmdx Jun 08 '14 at 23:42
  • UUID work very well on all platforms. They are standardized. – Thilo Jun 09 '14 at 00:44

1 Answers1

2

This method will generate a random number using arc4random_uniform(62) to choose a number between 0 and 61 and then chose a character from a string that holds numbers from 0 to 9 and alphabet letters both small and capital letters, respectively.

You could create an array with a string for all the characters you want to include, and randomly pick values. Or, alternatively you could take advantage of the ASCII encoding has mostly sequential character positions and you can fairly easily convert an ascii number to an NSString.

An integer between 48 and 57 is the numbers 0-9 in ASCII, 65 to 90 is A-Z and 97 to 122 is a-z: https://en.wikipedia.org/wiki/Ascii_table#ASCII_printable_code_chart

I was wondering when this approach will fail to generate a unique String and result in a repeated one. I searched about this topic and didn't find a satisfying answer.

It's often referred to as the "birthday problem". As long as your value is reasonably long (say, 20 characters), it is effectively impossible to have a collision. The world is more likely to be destroyed in the next 2 seconds than your app ever creating a collision.

I hope that you will help with me this since I am trying to use this method to generate unique IDs for use in my app.

Apple provides an API for generating unique IDs. You should use that instead of inventing your own system:

NSString *id = [NSUUID UUID].UUIDString;

That will give you a value like D19B40AA-322C-4ADF-BEF6-2EC4D4CE7BA8. It conforms to "Version 4" of the UUID standard — according to Wikipedia if you generate 1 billion UUIDs every second for the next 100 years, there is a 50% chance of getting two IDs that are the same.

If the UUID is longer than you want, you could grab a smaller part part of the string. Beware that the 4 at the start of the third block means this is a "version 4" UUID and is not a random value. Also the first character at the start of the 4th block is only has four possible values — so avoid or strip off those two characters if you want to grab a smaller part of the string for use as your random ID. See the wikipedia page on UUIDs for more detail.

Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
  • According to what you've said, using my approach will result in almost no collision since I'll be generating a 32-character string. So, how is it better to use the UUID Apple provides? Furthermore, will it be guaranteed that if the UUID was generated by many devices at the same moment it will still be unique? – ahmdx Jun 09 '14 at 00:15
  • @AhmedBadie Apple's UUID class is less likely to have bugs, and any bugs will be found/fixed by Apple, and generally it's less work for you. Other than that, there is no benefit. That's why I posted both options. :-) – Abhi Beckert Jun 09 '14 at 00:20
  • I edited the comment above to include this "Furthermore, will it be guaranteed that if the UUID was generated by many devices at the same moment it will still be unique?" In case you didn't see it. – ahmdx Jun 09 '14 at 00:25
  • 1
    "will it be guaranteed that if the UUID was generated by many devices at the same moment it will still be unique?" -> " according to Wikipedia if you generate 1 billion UUIDs every second for the next 100 years, there is a 50% chance of getting two IDs that are the same." – Thilo Jun 09 '14 at 00:51
  • Or to put it another way, if there are 500 trillion people using your app, you *might* get a collision. But your app doesn't have that many users, so don't worry about it. It will be unique. – Abhi Beckert Jun 09 '14 at 01:02
  • Thanks everyone and forgive me if my questions seemed stupid, I only wanted to make sure nothing will go wrong :) – ahmdx Jun 09 '14 at 01:12
  • @AhmedBadie no worries. By the way the security now back catalogue is a good source of this stuff. For example Episode #456 a few weeks ago is particularly relevant. https://www.grc.com/securitynow.htm It's a weekly podcast going back 9 years and generating random numbers is a common topic. – Abhi Beckert Jun 09 '14 at 01:30
  • Thanks @AbhiBeckert, I'll have a look at it. – ahmdx Jun 09 '14 at 02:07