CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jun 2008
    Location
    India
    Posts
    46

    Question generate 10-digit random unique numeric keys.

    How can I generate 10-digit random unique numeric keys..
    Can anybody suggest me an algorithm which is easy to implement or any other work around ..
    I jus LOVE non-verbal Communication

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: generate 10-digit random unique numeric keys.

    Digits should be from 0 to 9, or also include characters A - Z?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Jun 2008
    Location
    India
    Posts
    46

    Re: generate 10-digit random unique numeric keys.

    it shud be numeric..
    i.e from 0 to 9
    I jus LOVE non-verbal Communication

  4. #4
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: generate 10-digit random unique numeric keys.

    does a key that is generated stored somewhere?

    where exactly do you need such a large number for?

  5. #5
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: generate 10-digit random unique numeric keys.

    I'd suggest using a random generator and simply logging the keys that you have used to keep them unique...

    something like:

    Code:
    List<int> keys = new List<int>();
    
            private int NewKey()
            {
                Random ran = new Random(DateTime.Now.Millisecond);
    
                int key = 0;
                do
                {
                    key = ran.Next(1000000000, int.MaxValue);
    
                } while (keys.Contains(key));
    
                keys.Add(key);
                return key;
            }
    the 'keys' list would then be written to a file on exit and loaded on load...

    hope it helps...
    It's not a bug, it's a feature!

  6. #6
    Join Date
    Jun 2008
    Location
    India
    Posts
    46

    Re: generate 10-digit random unique numeric keys.

    I want to create registration keys of 10 digit long which will be unique and these nos will be stored into an excel file
    I jus LOVE non-verbal Communication

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: generate 10-digit random unique numeric keys.

    Quote Originally Posted by connect_fc
    I want to create registration keys of 10 digit long which will be unique and these nos will be stored into an excel file

    Why not just use a GUID? [not a 10 digit number, but guaranteed to never duplicate, and trivial to generate, and no need to keep track of all generated items (although for validation you will want that....)
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Jun 2008
    Location
    India
    Posts
    46

    Re: generate 10-digit random unique numeric keys.

    I guess GUID will give me alphanumeric keys which I dont want..
    I need numeric 10 digit reg key (in future it can be more than 10 & less than 15 also)
    I jus LOVE non-verbal Communication

  9. #9
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: generate 10-digit random unique numeric keys.

    Quote Originally Posted by foamy
    I'd suggest using a random generator and simply logging the keys that you have used to keep them unique...

    something like:

    Code:
    List<int> keys = new List<int>();
    
            private int NewKey()
            {
                Random ran = new Random(DateTime.Now.Millisecond);
    
                int key = 0;
                do
                {
                    key = ran.Next(1000000000, int.MaxValue);
    
                } while (keys.Contains(key));
    
                keys.Add(key);
                return key;
            }
    the 'keys' list would then be written to a file on exit and loaded on load...

    hope it helps...
    '

    int.MaxValue == 2,147,483,647. Thus you are only using a small range of the digit key

    I would suggest
    Code:
          List<long> keys = new List<long>();
    
          private long NewKey() {
             Random ran = new Random(DateTime.Now.Millisecond);
    
             int keyFirstPart = 0;
             int keySecondPart = 0;
             long result = 0;
             do {
                // first 8 numbers
                keyFirstPart = ran.Next(10000000, 99999999);
                // last 'n' numbers, in this case 2, easy to make it bigger up to 7
                keySecondPart = ran.Next(10, 99);
    
                //total key of 10 digits
                result = long.Parse(keyFirstPart.ToString() + keySecondPart.ToString());
             }
             while (keys.Contains(result));
    
             keys.Add(result);
             return result;
          }
    if you want the key to be greater than 10 digits jsut change the secondKeyPart generation into somethinf bigger
    Code:
    // last 'n' numbers, in this case 4
    keySecondPart = ran.Next(1000, 9999);
    
    //you total key is nog 12 digit

  10. #10
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: generate 10-digit random unique numeric keys.

    EDITED: I suck with a calculator
    Last edited by eclipsed4utoo; October 14th, 2008 at 10:06 AM.

  11. #11
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: generate 10-digit random unique numeric keys.

    Code:
    result = long.Parse(keyFirstPart.ToString() + keySecondPart.ToString());
    Talk about deliberately wasting resources....

    Just make the two variables long, and do a quick multiply and add!
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  12. #12

    Re: generate 10-digit random unique numeric keys.

    Or...

    Code:
    int maxdigits = 10; // Change to needed # of digits
    StringBuilder result = new StringBuilder();
    Random r = new Random(); // Seed with what you feel is appropriate
    for (int i = 0; i<=maxdigits; i++)
    {
       result.Append(r.Next(10)); // Append a number from 0 to 9
    }
    
    string key = result.ToString();
    If you need it in a different format, just convert. Otherwise, you can actually use zeros as starting digits.

    Now storing this in excel will require making sure the data format is correct or you'd lose the leading zeros.

    Just some advice from experience - use a GUID. Or a hash. Or something more complex. Depending on what you're trying to create, it's pretty simple for someone to brute force this type of identifier by simply looping through the possibilities.

  13. #13
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: generate 10-digit random unique numeric keys.

    Quote Originally Posted by dannystommen
    '

    int.MaxValue == 2,147,483,647. Thus you are only using a small range of the digit key

    I would suggest
    Code:
          List<long> keys = new List<long>();
    
          private long NewKey() {
             Random ran = new Random(DateTime.Now.Millisecond);
    
             int keyFirstPart = 0;
             int keySecondPart = 0;
             long result = 0;
             do {
                // first 8 numbers
                keyFirstPart = ran.Next(10000000, 99999999);
                // last 'n' numbers, in this case 2, easy to make it bigger up to 7
                keySecondPart = ran.Next(10, 99);
    
                //total key of 10 digits
                result = long.Parse(keyFirstPart.ToString() + keySecondPart.ToString());
             }
             while (keys.Contains(result));
    
             keys.Add(result);
             return result;
          }
    if you want the key to be greater than 10 digits jsut change the secondKeyPart generation into somethinf bigger
    Code:
    // last 'n' numbers, in this case 4
    keySecondPart = ran.Next(1000, 9999);
    
    //you total key is nog 12 digit
    Yeah, that did strike me as I was writing it, but I figured it would be a nice starting point... Anyway, nice solution
    It's not a bug, it's a feature!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured