Advertisement
aFilthy-Casual

Generate Unique Id (string)

Dec 6th, 2019
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | None | 0 0
  1.     private string ID()
  2.     {
  3.         string uniqueId = string.Empty;
  4.         string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+"; // All Alphabet Characters
  5.  
  6.         int idSize = 10; // Size Of Unique ID
  7.         int a_OR_0; //Determine if Char will be numeric or alphabetical
  8.  
  9.         for (int i = 0; i < idSize; i++)
  10.         {
  11.             a_OR_0 = Random.Range(0, 2);
  12.  
  13.             switch (a_OR_0)
  14.             {
  15.                 case 0: // letter
  16.                     {
  17.                         uniqueId += alphabet[Random.Range(0, alphabet.Length)];
  18.                         break;
  19.                     }
  20.                 case 1: // number
  21.                     {
  22.                         uniqueId += Random.Range(0, 9).ToString();
  23.                         break;
  24.                     }
  25. //The Random Function Seemed to prefer alphabetical so a second chance of numeric seems to balance this
  26.                 default: //number
  27.                     {
  28.                         uniqueId += Random.Range(0, 9).ToString();
  29.                         break;
  30.                     }
  31.             }
  32.         }
  33.  
  34.         return uniqueId;
  35.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement