tags:

views:

72

answers:

4

Hi everyone.

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

+3  A: 

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

Steve
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
yea, figured the z was a typo.
Steve
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
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
+1  A: 

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
Not guaranteed to be unique.
Itay
@Italy, but then neither is something with only 8 characters.
kenny
we are talking about a GUID... it IS UNIQUE
Bogdan
@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
@kenny - right, but you can at least guarantee to use all possibilities before overlapping for duplications which is not the case for GUID.
Itay
@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
@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
Ah crap, just realized that he doesn't need a GUID, he wants a full char incrementor for an identifier :)
Bogdan
+1  A: 

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
always returns 4848484848484848 // unique, choose by dice roll :)
Bogdan
didn't test is :), this should be a directing answer. I am not even sure if `(char)(keys[i]) - 10 + 'a'` could be done in C# :)
Itay
use LINQPad for snippets dude :)
Bogdan
maybe one day i will switch notepad to LINQPad ;)
Itay
It is fixed now - works like a charm :)
Itay
+1  A: 

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;
}
CodeInChaos