SoFunction
Updated on 2025-03-07

C# Chinese random number implementation method

This article describes the C# Chinese random number implementation method. Share it for your reference. The details are as follows:

/// <summary>
/// Random Chinese code/// </summary>
/// <returns></returns>
private string GetRndCh()
{
  gb = ;
 //Get GB2312 encoded page (table) object[] bytes = CreateRegionCode(4);
 //Calling the function to generate 4 random Chinese character encodings string[] str = new string[4];
  sb = new ();
 for (int i = 0; i < 4; i++)
 {
   //Decode Chinese characters based on the byte array encoded by Chinese characters   str[i] = ((byte[])(bytes[i], typeof(byte[])));
   ( str[i].ToString());
 }
 return  ();
 }
 /// <summary>
 /// Generate random Chinese character encoding /// </summary>
 /// <param name="strlength"></param>
 /// <returns></returns>
 private static object[] CreateRegionCode(int strlength)
 {
 //Define a string array to store the components of Chinese character encoding 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[] bytes = new object[strlength];
 for (int i = 0; i < strlength; i++)
 {
   //The first position of the area code   int r1 = (11, 14);
   string str_r1 = rBase[r1].Trim();
   //The second position code   rnd = new Random(r1 * unchecked((int)) + i);
   int r2;
   if (r1 == 13)
   {
   r2 = (0, 7);
   }
   else
   {
   r2 = (0, 16);
   }
   string str_r2 = rBase[r2].Trim();
   //The third position code   rnd = new Random(r2 * unchecked((int)) + i);
   //Replace random seeds   int r3 = (10, 16);
   string str_r3 = rBase[r3].Trim();
   //The fourth position code   rnd = new Random(r3 * unchecked((int)) + i);
   int r4;
   if (r3 == 10)
   {
   r4 = (1, 16);
   }
   else if (r3 == 15)
   {
   r4 = (0, 15);
   }
   else
   {
   r4 = (0, 16);
   }
   string str_r4 = rBase[r4].Trim();
   //Define the random Chinese character position code generated by two byte variable storage   byte byte1 = (str_r1 + str_r2, 16);
   byte byte2 = (str_r3 + str_r4, 16);
   //Storing two byte variables in byte array   byte[] str_r = new byte[] { byte1, byte2 };
   //Put the byte array of a Chinese character generated into the object array   (str_r, i);
 }
 return bytes;
}

I hope this article will be helpful to everyone's C# programming.