jeudi 20 juin 2019

Unique random alphanumeric characters based on client ID

I would like to generate unique alphanumeric max 50 character string based on a client ID. What is the chance of getting the same string by the way? I have more than one client, I don't want strings to be overlapped.

Here is what I planning to use:

static Random _Random = new Random();
static char[] _AllowedChars =
            Enumerable.Range(48, 10).Select(x => (char)x)
                .Concat(Enumerable.Range(65, 26).Select(x => (char)x))
                .Concat(Enumerable.Range(97, 26).Select(x => (char)x))
                .ToArray();
static string PseudoUniqueString(int maxLength = 50)
            => new string(Enumerable.Range(1, Math.Max(1, maxLength))
                .Select(x => _AllowedChars[_Random.Next(_AllowedChars.Length)])
                .ToArray());

Is there a way to add client ID (let's say 1) at the beginning of this string? I don't come up with another solution. Advice needed.




Aucun commentaire:

Enregistrer un commentaire