7

How can I always generate a 17 character unique alphanumeric data with c#.

Ryan
  • 5,644
  • 3
  • 38
  • 66
Thomas
  • 33,544
  • 126
  • 357
  • 626

3 Answers3

6

Generate new Guid, and divide it 17 times by modulo 62. Each number you get is an index in char array described above ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ1234567890"). By using Guid you can guarantee that your value is as unique as Guid is.

If you are concerned with losing unique bits you may hash GUID with MD5 like this:

Guid guidValue = Guid.NewGuid();
MD5 md5 = MD5.Create();
Guid hashed = new Guid(md5.ComputeHash(guidValue.ToByteArray()));

UPDATE Guid format

According to this document (RFC 4122) and comparing with GUIDs generated by C#, they are of random type.

This type has the following pattern: xxxxxxxx-xxxx-4xxx-Vxxx-xxxxxxxxxxxx, where

  • x is random number and
  • V is a number with bit layout as 10yy, where yy are two random bits.

So, here we have 122 random bits out of 128. So, in terms of uniqueness the Guid is simply a large random number and you are free to use any other random-number-generation algorithm that is capable to produce 88-bit random number (for example RNGCryptoServiceProvider).

Of course the method used to generate Guids may change in future versions of framework, but at the moment the Guid.NewGuid() looks like cheap random number generator in terms of code.

Artemix
  • 2,113
  • 2
  • 23
  • 34
  • +1, because the GUID is as unique as they get and this algorithms conserves as much of that uniqueness as possible. – Cosmin Prund Apr 06 '11 at 13:12
  • 1
    @Cosmin: You can't guarantee that this conserves as much of the GUID's uniqueness as possible without knowing *how* the GUID was generated. This algorithm could be losing all the bits that guarantee uniqueness (I'm not saying that it does, but it's a theoretical possibility). – LukeH Apr 06 '11 at 13:18
  • @LukeH: noticed that after the post... I'd tackle that problem by taking the remainder of the GUID divided by a large 108 bits prime number. Or by doing some partial XOR-ing to make sure I'm working with 12 bytes that are affected by all bits in the initial 16 bytes. Of course, it can't possibly have the same properties as the initial GUID, but it's going to be pretty good. – Cosmin Prund Apr 06 '11 at 13:50
4

You could try... http://msdn.microsoft.com/en-us/library/system.io.path.getrandomfilename.aspx

The GetRandomFileName method returns a cryptographically strong, random string that can be used as either a folder name or a file name.

Or whatever you want.

BenCr
  • 5,991
  • 5
  • 44
  • 68
  • Note that this method has limitations: with each call you get only 11 lower-case alphanumeric characters since it provides you with the file/folder name in 8.3 format (i.e. "qwer176d.d9s"). – Artemix Apr 06 '11 at 15:17
2

You can have hardcoded character set -

char[] chars = new char[62];
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ1234567890".ToCharArray();

Then using random number between 0 to 62, you can get random characters each time and append to your string.

However, you wont be able to guarantee that sometime down the lane, you will not be getting duplicate alphanumeric string too.

As an alternative, if possible, why dont you use GUID?

Sachin Shanbhag
  • 54,530
  • 11
  • 89
  • 103
  • 1
    If a GUID can "guarantee" uniqueness within its range (128 bits), then why couldn't a 17-character A-Za-z0-9 string also "guarantee" uniqueness withing its own range (roughly 100 bits)? With the right algorithm you could generate 2**100 unique strings before needing to repeat yourself. – LukeH Apr 06 '11 at 13:07
  • 1
    The 17-char string is generated by you. If you have an algorithm to check for uniqueness, then it can be guaranteed. Else no. – Sachin Shanbhag Apr 06 '11 at 13:28