C#生成隨機姓名,隨機漢字的產生


 public static object[] CreateRegionCode(int strlength, bool isRandomCount = false)
 {
     if (isRandomCount)
     {
         Random random = new Random();
         strlength = random.Next(1, strlength + 1);
     }

     //定義一個字符串數組儲存漢字編碼的組成元素
     string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
     Random rnd = new Random();
     //定義一個object數組用來
     object[] bytes = new object[strlength];
     /*每循環一次產生一個含兩個元素的十六進制字節數組,並將其放入bject數組中
      每個漢字有四個區位碼組成
      區位碼第1位和區位碼第2位作為字節數組第一個元素
      區位碼第3位和區位碼第4位作為字節數組第二個元素
     */

     for (int i = 0; i < strlength; i++)
     {
         //區位碼第1位
         int r1 = rnd.Next(11, 14);
         string str_r1 = rBase[r1].Trim();
         //區位碼第2位
         rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更換隨機數發生器的種子避免產生重復值
         int r2;
         if (r1 == 13)
         {
             r2 = rnd.Next(0, 7);
         }
         else
         {
             r2 = rnd.Next(0, 16);
         }
         string str_r2 = rBase[r2].Trim();
         //區位碼第3位
         rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
         int r3 = rnd.Next(10, 16);
         string str_r3 = rBase[r3].Trim();
         //區位碼第4位
         rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
         int r4;
         if (r3 == 10)
         {
             r4 = rnd.Next(1, 16);
         }
         else if (r3 == 15)
         {
             r4 = rnd.Next(0, 15);
         }
         else
         {
             r4 = rnd.Next(0, 16);
         }
         string str_r4 = rBase[r4].Trim();
         //定義兩個字節變量存儲產生的隨機漢字區位碼
         byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
         byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
         //將兩個字節變量存儲在字節數組中
         byte[] str_r = new byte[] { byte1, byte2 };
         //將產生的一個漢字的字節數組放入object數組中
         bytes.SetValue(str_r, i);
     }
     return bytes;
 }
姓氏獲取
  public static string GetSurname()
  {
      Random random = new Random();
      int index = random.Next(0, surname.Count());
      return surname[index];
  }

  public static List<string> surname = new List<string>() {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "羿", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "宿", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "滿", "",
  "", "", "", "", "", "祿", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  "", "", "", "", "", "", "", "", "", "",
  "萬俟", "司馬", "上官", "歐陽", "夏侯", "諸葛", "聞人", "東方", "赫連", "皇甫", "尉遲", "公羊",
  "澹台", "公冶", "宗政", "濮陽", "淳於", "單於", "太叔", "申屠", "公孫", "仲孫", "軒轅", "令狐",
  "鍾離", "宇文", "長孫", "慕容", "鮮於", "閭丘", "司徒", "司空", "丌官", "司寇", "子車", "微生",
  "顓孫", "端木", "巫馬", "公西", "漆雕", "樂正", "壤駟", "公良", "拓拔", "夾谷", "宰父", "谷梁",
  "段干", "百里", "東郭", "南門", "呼延", "羊舌", "梁丘", "左丘", "東門", "西門", "南宮"};
//使用
for
(int j = 0; j < 20; j++) { // string surname = ChineseName.GetSurname(); //獲取GB2312編碼頁(表) Encoding gb = Encoding.GetEncoding("gb2312"); //調用函數產生4個隨機中文漢字編碼 object[] bytes = ChineseName.CreateRegionCode(2, true); //根據漢字編碼的字節數組解碼出中文漢字 string name = string.Empty; for (int i = 0; i < bytes.Length; i++) { name += gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[]))); } Console.WriteLine($"{surname} {name}"); Thread.Sleep(100); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM