Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate 8 digit uinque id in c#

Tags:

c#

asp.net

I need to generate 8 digit unique id in c#. On my site a user will register and I need to generate a unique id for him in c# code(I don't want this logic in DB), after inserting the id I need to save it in database.

Edit: I need that numbers to be generated in random manner everytime.

like image 246
Rocky Singh Avatar asked Dec 12 '10 11:12

Rocky Singh


People also ask

How do you generate a 8 digit random number?

Random rnd = new Random(); int myRandomNo= rnd. Next(10000000, 99999999); // creates a 8 digit random no.

How do you generate unique identifiers?

The simplest way to generate identifiers is by a serial number. A steadily increasing number that is assigned to whatever you need to identify next. This is the approached used in most internal databases as well as some commonly encountered public identifiers.


3 Answers

Although not 8 digits, I'd use a GUID for that purpose:

var id = Guid.NewGuid().ToString()

From Wikipedia:

Ideally, a GUID will never be generated twice by any computer or group of computers in existence. The total number of unique keys (2^128 or 3.4×10^38 - in relation there are about 1.33×10^50 atoms on earth) is so large that the probability of the same number being generated twice is extremely small, and certain techniques have been developed to help ensure that numbers are not duplicated

like image 105
Alon Gubkin Avatar answered Sep 28 '22 12:09

Alon Gubkin


If you don't mind the IDs being predictable, I'd go with Vlad's suggestion.

Otherwise, I'd generate a random number in the required range and just try to insert it in the database... if you get an exception due to the uniqueness constraint being violated in the database (and that constraint absolutely should be there) then try again. Keep trying until it either works or you've gone round a certain number of times. (It's highly unlikely that you'll fail 100 times for example - unless you've got a bug elsewhere, in which case an exception is preferable to an infinite loop.)

So this isn't generating the ID in the database - but it's verifying the uniqueness in the database, which is after all the ultimate "source of truth".

If you don't need cryptographically securely generated IDs, then simply using Random.Next(100000000) will generate you a value in the range [0, 99999999]. If you don't want any values which need leading 0s to get to 8 digits, simply use Random.Next(10000000, 100000000) which will give you a smaller range of possible values, but you won't need to worry about them having fewer than 8 digits.

Using Random properly has a few "gotchas" - see my article about it for more details.

like image 29
Jon Skeet Avatar answered Sep 28 '22 12:09

Jon Skeet


You could try to implement a method that generates a random number but you have always to check if it is already in database.

    static void Main(string[] args)
    {
        HashSet<string> numbers = new HashSet<string>();

        for (int i = 0; i < 100; i++)
        {
            numbers.Add(GenerateRandomNumber(8));
        }

        Console.WriteLine(numbers.Count == 100);
        Console.ReadLine();
    }

    static Random random = new Random();

    static string GenerateRandomNumber(int count)
    {
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < count; i++)
        {
            int number = random.Next(10);
            builder.Append(number);
        }

        return builder.ToString();
    }
like image 42
as-cii Avatar answered Sep 28 '22 12:09

as-cii