1

I want to Generate a Unique Numeric ID for transactions tracking code .

This is my current method:

public string Token_Generator()
    {
        Random generator = new Random();         
        String r = generator.Next(0, 1000000).ToString("D6");
        return r;
    }

I also tried Random((int)System.DateTime.Now.Ticks )

But when i call this method in a For Loop and Call it like 10 times , i get Duplicate IDs .

This is my result :

308860
308860
308860
308860
308860
308860
308860
844391
844391

As You can see, The results are Duplicated and i cant work with it . And i also want to generate it with length of 20 character as string.

And in one of my last projects i tried this code for similar situations:

public string tokenGenerator()
    {
        string token = "";
        for (int i = 0; i < 24; i++)
        {
            token += Guid.NewGuid().ToString().Replace("-", string.Empty).Substring(0, 5);
        }
        return token;
    }

Result :

ec811cad8f9f95ed5f893ebc4e4f7459d8993cb17d5fa93d54fbff0c29e172080fad21a0f37c2d61d3a88c50f6ef278770e30793e955ab58fd94de4c

But it contains chars and i want it to be Numeric.

Can you help me to write a method to Generate Completely Unique and NumericIwhit customizable length ?

Like :

Public string Token(int length){
     Return token;
} 

Thanks :)

Selena MG
  • 43
  • 5

0 Answers0