IP Addressing Scheme Suggestion

November 5th, 2012 admin Posted in Uncategorized No Comments »

IP Addressing Scheme


A quick uniqueID generator that does not “look” like a guid.

June 21st, 2009 admin Posted in Uncategorized No Comments »

While toying around I came up with this quick unique ID generator that will take a {n} length series of Guids, cat them together and then replace any numeric values with random (on each replacement) alphanumeric characters and lastly, remove any dashes.  The small method allows you to mix uppercase and lowercase letters at random.  It sems from generating unique ID values for resetting a users password (a string to pass in via the URL) while not wanting to first base64 or URL the string nor did I want a "guid" looking string as a lot of websites use.

Here is the code; as always I welcome any suggestions in improving my work.

  1.  
  2. /// <summary>
  3. /// Generates the random ID.
  4. /// </summary>
  5. /// <param name="GuidIterations">The GUID iterations.</param>
  6. /// <param name="mixItUp">if set to <c>true</c> [mix it up].</param>
  7. /// <returns></returns>
  8. string GenerateRandomID(int GuidIterations, bool mixItUp)
  9. {
  10. Random rChar = new Random((int)System.DateTime.Now.Ticks);
  11. StringBuilder guidChain = new StringBuilder(GuidIterations * 36);
  12. StringBuilder uniqueID = new StringBuilder(GuidIterations * 36);
  13. string newChar;
  14. for(int i = 0; i < GuidIterations; ++i)
  15. guidChain.Append(Guid.NewGuid().ToString());
  16. for(int i = 0; i < guidChain.Length; ++i)
  17. if(char.IsNumber(guidChain[i]))
  18. {
  19. newChar = ((char)rChar.Next(97, 122)).ToString();
  20. uniqueID.Append(mixItUp ? (rChar.NextDouble() > 0.5 ? newChar : newChar.ToUpper()) : newChar);
  21. }
  22. else uniqueID.Append(guidChain[i]);
  23. return uniqueID.Replace("-", "", 0, uniqueID.Length).ToString();
  24. }
  25.  

An example of calling the method:

  1.  
  2. using System;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace c3
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Console.WriteLine("UniqueID " + GenerateRandomID(1, false));
  14. Console.WriteLine("UniqueID " + GenerateRandomID(1, true));
  15. Console.WriteLine("UniqueID " + GenerateRandomID(2, false));
  16. Console.WriteLine("UniqueID " + GenerateRandomID(2, true));
  17. Console.ReadLine();
  18. }
  19. }
  20. }
  21.  

which would generate output similar to this:

UniqueID dcfcftlddkdaxffcadeebbbtxyutafvg
UniqueID cbLXtytGuefewbLeGxiNaeqieevdVdOd
UniqueID dcftlkccxbtbxbyutvcacgmumfdeqwylcggaxweiqafnleabawqeafdpiuvfboev
UniqueID bccbabLXtytcGueawLGxdieNaqivceVOmamFdcddhMbMRkeufeqtKSbncQnfHbbt

The first line is all lowercase, and 1 GUID length, the second, the same length but randomly mixed with uppercase characters.