Query about generating random but unique keys of 6 digits?
-
hi every buddy, I'm working in an ASP .NET project and in that I need to generate a unique key which is to be used instead of the primary key in the database of 6 digits and it should be random in nature yet it should be unique ofcourse. Actually the client doesnt want to issue numbers such as 000001, 000002... he wants it to be like 872731. The problem in my mind is that how are we gonna generate these unique but random numbers, coz whats bugging me is: WE would have to check for the keys already taken and 1) its v inefficient and 2) it could have problems with concurrency. So I really need to guidelines from you people, I would definately appreciate this. Thank you.
Rocky Success is a ladder which you can't climb with your hands in your pockets.
-
hi every buddy, I'm working in an ASP .NET project and in that I need to generate a unique key which is to be used instead of the primary key in the database of 6 digits and it should be random in nature yet it should be unique ofcourse. Actually the client doesnt want to issue numbers such as 000001, 000002... he wants it to be like 872731. The problem in my mind is that how are we gonna generate these unique but random numbers, coz whats bugging me is: WE would have to check for the keys already taken and 1) its v inefficient and 2) it could have problems with concurrency. So I really need to guidelines from you people, I would definately appreciate this. Thank you.
Rocky Success is a ladder which you can't climb with your hands in your pockets.
Why do people insist on this anti-pattern? :( You will likely need to persist all the id's in a table, then just create random number, check for uniqueness on table, repeat if necessary, add newly created number. For concurrency, you will probably need to lock the table or something.
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008) -
hi every buddy, I'm working in an ASP .NET project and in that I need to generate a unique key which is to be used instead of the primary key in the database of 6 digits and it should be random in nature yet it should be unique ofcourse. Actually the client doesnt want to issue numbers such as 000001, 000002... he wants it to be like 872731. The problem in my mind is that how are we gonna generate these unique but random numbers, coz whats bugging me is: WE would have to check for the keys already taken and 1) its v inefficient and 2) it could have problems with concurrency. So I really need to guidelines from you people, I would definately appreciate this. Thank you.
Rocky Success is a ladder which you can't climb with your hands in your pockets.
Use a GUID.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Why do people insist on this anti-pattern? :( You will likely need to persist all the id's in a table, then just create random number, check for uniqueness on table, repeat if necessary, add newly created number. For concurrency, you will probably need to lock the table or something.
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008)leppie wrote:
Why do people insist on this anti-pattern? [Frown]
hmm didnt get that one! honestly. now comming to the point. I really dont think locking a table would be a great idea from an ASP .NET app. I may be wrong. Do u think I can generate the random numbers from TSQL?
Rocky Success is a ladder which you can't climb with your hands in your pockets.
-
Use a GUID.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
yea well I had that in mind too but how will I convert that to a six digit number?
Rocky Success is a ladder which you can't climb with your hands in your pockets.
Ohh sorry. I haven't noticed the 6 digit requirement. You need follow what leppie said then. It will tough to make it reliable.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Ohh sorry. I haven't noticed the 6 digit requirement. You need follow what leppie said then. It will tough to make it reliable.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
no its ok, :) but I'm still perplexed about locking the table from this asp .net app. I think it has the potential of bring the whole application to its knees!. :(
Rocky Success is a ladder which you can't climb with your hands in your pockets.
It will only lock the table while generating the number, that should take 10ms, so that should work unless you need to do this 100 times a seconds. And in that case 6 digits wont cover uniqueness, you will run out in 10 minutes. That's why it is an anti-pattern (just wrong from the start).
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008) -
It will only lock the table while generating the number, that should take 10ms, so that should work unless you need to do this 100 times a seconds. And in that case 6 digits wont cover uniqueness, you will run out in 10 minutes. That's why it is an anti-pattern (just wrong from the start).
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008) -
hi every buddy, I'm working in an ASP .NET project and in that I need to generate a unique key which is to be used instead of the primary key in the database of 6 digits and it should be random in nature yet it should be unique ofcourse. Actually the client doesnt want to issue numbers such as 000001, 000002... he wants it to be like 872731. The problem in my mind is that how are we gonna generate these unique but random numbers, coz whats bugging me is: WE would have to check for the keys already taken and 1) its v inefficient and 2) it could have problems with concurrency. So I really need to guidelines from you people, I would definately appreciate this. Thank you.
Rocky Success is a ladder which you can't climb with your hands in your pockets.
The problem of generating non-repeating 6-digit numbers that appear random can be solved with cyclic groups: Start with any 6-digit number. This is your first key. Pick a random prime number (any will work, but a 6-digit prime between 300000 and 700000 would be best). Every time you add the prime number to the key (and truncate to six digits), you get the next key. The beauty of this approach is that you'll go through EVERY 6-digit number before it repeats.