0

I would like to create unique value generator that would work like this: I have 8 places for chars and it would create values like this:

00000001 . 0000000z . 00000010 . 0000001z

etc. so it would create values from 00000001 to zzzzzzzz.

I have only 8 places because this is the size of the field in the database and I can't change it.

Thanks in advance

kamilwydrzycki
  • 451
  • 2
  • 6
  • 16
  • 3
    Thanks for letting us know. You should consider editing your question so that it is actually a question rather than a statement. We'd at least like to see that you tried. – Jeff Yates Oct 21 '10 at 13:21
  • Uppercase and lowercase or just lowercase? – Aliostad Oct 21 '10 at 13:23
  • 2
    @Jeff Yates: Occam's razor I think suggests that he wants a base 3 counting system with the number 2 replaced by a z. It is I believe the simplest system that fits the sequence shown. :) We should have a competition to see who can guess the question correctly. :) – Chris Oct 21 '10 at 13:33
  • I will note that if this is essentially talking about using different bases then http://stackoverflow.com/questions/686847/base-convert-in-net might be of interest as a general case. It's accepted answer contains general methods for converting between bases up to 36. If you wanted to differentiate between upper and lower case letters (similar to base64 encoding) then you could easily modify it up to 62 and add any extra characters you feel like to increase the minimum base size. – Chris Oct 21 '10 at 15:54

4 Answers4

1

Use the bultin random() function, then convert your result to hex once you are done.

  • 1
    The OP seems to want more than hex. However, they didn't ask a question so I don't know what it is they really want. – Jeff Yates Oct 21 '10 at 13:23
  • yea, figured the z was a typo. –  Oct 21 '10 at 13:24
  • I think after the latest edit there are too many Zs to be a typo... Which makes it ironic that yours is the highest scored answer. :) – Chris Oct 21 '10 at 15:47
  • Also a built in random function is more likely to fail on uniqueness than a sequential generation of numbers (the reasons for which I hope are obvious). I'm not sure randomness should be bought in here. – Chris Oct 21 '10 at 15:56
1

Are you sure you're not looking for a GUID?

Same number of chars, different structure: 550e8400-e29b-41d4-a716-446655440000 You can create a new GUID:

System.Guid.NewGuid().ToString(""N"")

Split it every 8 chars and append a dot, like this:

string getUnique()
{
    char[] initial = System.Guid.NewGuid().ToString("N").ToCharArray();
    string result="";

    for(int i=0; i<initial.Count(); i++){
        result=result + initial[i];
        if((i+1)%4==0 && (i+1)!=initial.Count()){
            result = result + ".";
        }
    }
    return result;
}
Bogdan
  • 3,055
  • 1
  • 22
  • 20
  • @Italy, but then neither is something with only 8 characters. – kenny Oct 21 '10 at 13:44
  • we are talking about a GUID... it IS UNIQUE – Bogdan Oct 21 '10 at 13:45
  • @Bodgan - it will be unique with a very very very high probability. but it is not (and if you only use part of it you loose most of its so called `uniqueness`). – Itay Karo Oct 21 '10 at 13:56
  • @kenny - right, but you can at least guarantee to use all possibilities before overlapping for duplications which is not the case for GUID. – Itay Karo Oct 21 '10 at 13:57
  • @Itay 2^128 for me means unique (also see http://stackoverflow.com/questions/2977593/is-it-safe-to-assume-a-guid-will-always-be-unique), and as you can see in the code, the GUID is used in its entirety – Bogdan Oct 21 '10 at 13:59
  • @Bogdan - once you chop it to 8 chars it is much less than 2^128 (it is 2^32) and still - even with very low probability it is not unique even for 2^128. – Itay Karo Oct 21 '10 at 14:02
  • Ah crap, just realized that he doesn't need a GUID, he wants a full char incrementor for an identifier :) – Bogdan Oct 21 '10 at 14:07
1

something like this

public class UniqueKeyMaker
{
    private int[] keys = new int[8];

    public void Reset()
    {
        for (int i = 0; i < keys.Length; i++)
            keys[i] = 0;
    }

    public string NextKey()
    {       
        string key = getCurrentKey();
        increment();
        return key;
    }

    private void increment()
    {
        int i = 7;
        while (keys[i] == 35)
        {
            keys[i] = 0;
            i--;
        }

        keys[i]++;
    }

    private string getCurrentKey()
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 8; i++)
        {
            if (keys[i] < 10)
                sb.Append((char)(keys[i] + (int)'0'));
            else
                sb.Append((char)(keys[i] - 10 + (int)'a'));
        }
        return sb.ToString();
    }
}
Itay Karo
  • 17,924
  • 4
  • 40
  • 58
1

You just want to encode an int into a base 36 system. This might work like like following(probably has a few small mistakes since it's notepad code):

string IntToID(long id)
{
    string result="";
    Contract.Require(id>=0);
    while(id>0)
    {
       int digit=id%36;
       char digitChar;
       if(digit<10)
         digitChar='0'+digit;
       else
         digitChar='a'+(digit-10);
       result+=digitChar;
       id/=36;
    }
    result=result.PadLeft('0',8);
    return result;
}
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262