Generate unique id of length 8

348 Views Asked by At

I'm looking for a solution which works well to generate unique id of length 8. Volume is not significant, it is jusy 1M per month. Can I not generate v4 version of uuid and pick last 8 from it. Since the volume is just about 5 request per second, it is likely to be alright. Any comments on the approach and pointer to other approaches are welcome. I have already tried base 64 encoded, short uuid etc. Looking for reasons why last 8 of uuid won't work for low tps.

1

There are 1 best solutions below

0
Ozan BAYRAM On

You may use something like this. I am using this for https://cut.lu I am checking uniqueness towards db. If exists, getting new one. This is quick and dirty but works.

public static string GenerateSlug(int length)
{
    string allowedCharsURL = "abcdefghijklmnopqrstuvwxyz1234567890";
    //$-_.+!*'(),

    //unwise in URI      = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"
    //reserved in URI    = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","

    string res = "";
    Random rnd = new Random();
    while (0 < length--)
        res += allowedCharsURL[rnd.Next(allowedCharsURL.Length)];
    return res;
}