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.
Use Sequential Guids! Reduces the probability of a clash where guids already have a low probability of clashing and also means you can order your data by the Guid, representing the time of insertion.
You can put this method in the base class for customers, or all entities and have it generated automagically in the base constructor on instatiation.
You can also use my identify generator. But it's not integer one unfortunately. Make sure you have case sensitive ID column in db otherwise change the character range. The identifier is url friendly. Based on partial DateTime Ticks (10 characters) and partial Random (6 characters). It's not sortable so use AddedDate column instead to get a row sequence. Use
varchar(16)
column type andSQL_Latin1_General_CP1_CS_AS
collation.Unit Test:
Good luck.
You can use
Random Class
Always remember that there is no technique to generate a unique Random number without checking existing values in database
You must have some information regarding previous values
You could try to implement a method that generates a random number but you have always to check if it is already in database.
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 useRandom.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.Why not just keep the last allocated number and increase it by 1 when allocating a new ID? Interlocked.Increment might be useful. You can pad the number with zeroes at the left to the 8 digits. Using
int
as backing type should be enough.Edit: if you want the number to look random, just store in the DB not the allocated sequential numbers themselves, but use some bijective mapping. For example, you can store
7461873*ID + 17845612
instead. This guarantees the uniqueness and looks random.By the way, this is similar how the random number generators usually work (only they don't use the sequential number, but rather the result of previous calculation).