Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate Unique alphanumeric?

Tags:

c#

random

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

like image 487
Thomas Avatar asked Apr 06 '11 12:04

Thomas


People also ask

How do I get unique alphanumeric codes in Excel?

Generate chars programmatically To generate an array with all uppercase letters A-Z, which map to ASCII 65-90: =CHAR(SEQUENCE(26,1,65,1)) // returns {"A","B","C",...} To generate lowercase letters a-z, which correspond to ASCII 97-122: =CHAR(SEQUENCE(26,1,97,1)) // returns {"a","b","c",...}

How do you generate random unique alphanumeric strings?

There are many ways to generate a random, unique, alphanumeric string in PHP which are given below: Using str_shuffle() Function: The str_shuffle() function is an inbuilt function in PHP and is used to randomly shuffle all the characters of a string passed to the function as a parameter.

How do you create an alphanumeric sequence?

You have to create 1 sequence and 1 transforming function: CREATE SEQUENCE num_seq START WITH 6500000000 INCREMENT BY 1 MAXVALUE 9099999999; FUNCTION next_id(seq_name) RETURN VARCHAR2 IS x VARCHAR2(28); BEGIN EXECUTE IMMEDIATE 'SELECT TRIM(TO_CHAR(' || seq_name || '.


3 Answers

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.

like image 74
Artemix Avatar answered Sep 30 '22 10:09

Artemix


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.

like image 41
BenCr Avatar answered Sep 30 '22 10:09

BenCr


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?

like image 36
Sachin Shanbhag Avatar answered Sep 30 '22 08:09

Sachin Shanbhag