Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate an ID that is garunteed to be unique? [duplicate]

Tags:

c#

I need to garuntee the uniqueness of an ID. Even if there's a 1 in 1 trillion chance of there being a dupe, I cannot use it.

But I don't really feel like going to the ends of the earth to find a solution either. I have spent some time looking into this, and everything I have found to date isn't able to garuntee me a unique ID.

So I thought I would use something very simple, like this:

    public static string GenerateItemID()
    {
        string year = DateTime.Now.Year.ToString(), 
            month = DateTime.Now.Month.ToString(), 
            day = DateTime.Now.Day.ToString(), 
            hour = DateTime.Now.Hour.ToString(), 
            minute = DateTime.Now.Minute.ToString(), 
            second = DateTime.Now.Second.ToString(), 
            millisecond = DateTime.Now.Millisecond.ToString();

        return year + month + day + hour + minute + second + millisecond;
    }

My logic here is: each new UI that is generated can not possibly be the same as any of the previous ones. But then I wondered if it were possible for a PC to generate two or more of these ID's within the same millisecond? Naturally, I then looked in Intellisense for a Nanosecond property, but it doesn't exist.

How can I generate an ID that is garunteed to be unique?

like image 502
SE13013 Avatar asked Jul 05 '15 14:07

SE13013


People also ask

How do I create a unique ID?

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.

How do I automatically generate unique IDs in Excel?

You can generate a unique value using a formula in the spreadsheet. An ID must be a value, not a formula, though, so copy (Ctrl+C) and paste as plain text (Shift+Ctrl+V) the result of the formula calculation into the cell meant to contain the new ID. That's all there is to it!

Can you get duplicate UUID?

While the probability that a UUID will be duplicated is not zero, it is close enough to zero to be negligible. Thus, anyone can create a UUID and use it to identify something with near certainty that the identifier does not duplicate one that has already been, or will be, created to identify something else.

Are UUID always unique?

UUIDs are handy for giving entities their own special names, for example, in a database. There are several ways to generate them, including methods based on time, MAC addresses, hashes, and random numbers, but they make the same promise: no two are identical. Each one is unique across space and time.


2 Answers

var id = Guid.NewGuid(); 

I believe this will create a fairly unique id.

like image 175
George Harnwell Avatar answered Oct 07 '22 02:10

George Harnwell


Let use GUID.

Refer here: https://msdn.microsoft.com/en-us/library/system.guid.newguid(v=vs.110).aspx

Hope this help.

like image 25
hungndv Avatar answered Oct 07 '22 03:10

hungndv