Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Query about generating random but unique keys of 6 digits?

Query about generating random but unique keys of 6 digits?

Scheduled Pinned Locked Moved C#
databasehelpcsharpquestionlounge
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rocky
    wrote on last edited by
    #1

    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.

    L N A 3 Replies Last reply
    0
    • R Rocky

      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.

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      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)

      R 1 Reply Last reply
      0
      • R Rocky

        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.

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        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

        R 1 Reply Last reply
        0
        • L leppie

          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)

          R Offline
          R Offline
          Rocky
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • N N a v a n e e t h

            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

            R Offline
            R Offline
            Rocky
            wrote on last edited by
            #5

            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.

            N 1 Reply Last reply
            0
            • R Rocky

              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.

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              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

              R 1 Reply Last reply
              0
              • N N a v a n e e t h

                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

                R Offline
                R Offline
                Rocky
                wrote on last edited by
                #7

                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.

                L 1 Reply Last reply
                0
                • R Rocky

                  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.

                  L Offline
                  L Offline
                  leppie
                  wrote on last edited by
                  #8

                  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)

                  R 1 Reply Last reply
                  0
                  • L leppie

                    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)

                    R Offline
                    R Offline
                    Rocky
                    wrote on last edited by
                    #9

                    ok so that explains it!, thanks just one more question: hope u can answer that. How can we lock the table from ASP .NET, I've been searching over that as well. Any tips?

                    Rocky Success is a ladder which you can't climb with your hands in your pockets.

                    1 Reply Last reply
                    0
                    • R Rocky

                      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.

                      A Offline
                      A Offline
                      Alan Balkany
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups