Click here to Skip to main content
16,018,650 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello i want to generate a unique id just like this

#OWD-716-46324

how can i achieve it
Posted
Comments
OriginalGriff 9-Aug-13 5:04am    
We can't answer that - we have no idea how this is supposed to differ from the previous one, and how it differs from the next one.
[no name] 9-Aug-13 5:05am    
Is the format constant like 1st special characters and then capital letters then followed by numbers separated by "-"?
Ahmad Abd-Elghany 9-Aug-13 5:11am    
its always # at 1st char , three capital letters followed by "-" followed by numbers .

1 solution

Hello Ahmad,
You can use this code --


C#
private string GetRandomString()
       {
           Random rand = new Random((int)DateTime.Now.Ticks);
           int randomIndex1 = rand.Next(100, 999);
           int randomIndex2 = rand.Next(10000, 99999);
           string randstring = RandomString(3);

           string result = "#" + randstring + "-" + randomIndex1.ToString() + "-" + randomIndex2.ToString();
           return result;
       }

       private static Random random = new Random((int)DateTime.Now.Ticks);
       private string RandomString(int size)
       {
           StringBuilder builder = new StringBuilder();
           char ch;
           for (int i = 0; i < size; i++)
           {
               ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
               builder.Append(ch);
           }

           return builder.ToString();
       }



U can also refer this link

http://stackoverflow.com/questions/1122483/c-sharp-random-string-generator[^]
 
Share this answer
 
Comments
Ahmad Abd-Elghany 9-Aug-13 5:48am    
Big Kiss :D
[no name] 9-Aug-13 5:50am    
+5.
Punamchand Dhuppad 9-Aug-13 5:54am    
Thank you Ahmad ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900