国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

使用c#生成隨機數總結

2023-05-16 12:36:05
字體:
來源:轉載
供稿:網友

private static char[] constant = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

    [WebMethod(Description="得到數字加英文的隨機數,參數:隨機數長度,返回值:一個字符串!")]
    public string pxkt_GetCharFont(int strLength)
    {
         System.Text.StringBuilder   newRandom   =   new   System.Text.StringBuilder(62);   
        Random   rd=   new   Random();   
        for(int   i=0;i<strLength;i )   
        {   
          newRandom.Append(constant[rd.Next(62)]);   
        }   
        return   newRandom.ToString();   
    }


    private static char[] englishchar = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

    [WebMethod(Description = "得到英文的隨機數,參數:隨機數長度,返回值:一個字符串!")]

    public string GetEnglishChar(int strLength)
    {
        System.Text.StringBuilder newRandom = new System.Text.StringBuilder(52);
        Random rd = new Random();
        for (int i = 0; i < strLength; i )
        {
            newRandom.Append(englishchar[rd.Next(52)]);
        }
        return newRandom.ToString();
    
    }


    private static char[] numchar = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

    [WebMethod(Description = "得到數字的隨機數,參數:隨機數長度,返回值:一個字符串!")]
    public string GetNumChar(int strLength)
    {

        System.Text.StringBuilder newRandom = new System.Text.StringBuilder(10);
        Random rd = new Random();
        for (int i = 0; i < strLength; i )
        {
            newRandom.Append(numchar[rd.Next(10)]);
        }
        return newRandom.ToString();

    }



    [WebMethod(Description = "得到中文的隨機數,參數:隨機數長度,返回值:一個字符串!")]
    public string GetChineseChar(int strLength)
    {

        System.Text.StringBuilder newRandom = new System.Text.StringBuilder();
        //獲取GB2312編碼頁(表) 
        System.Text.Encoding gb = System.Text.Encoding.GetEncoding("gb2312");
        //調用函數產生I個隨機中文漢字編碼 
        object[] bytes = CreateRegionCode(strLength);
        
        for (int i = 0; i < strLength; i )
        {
            //根據漢字編碼的字節(jié)數組解碼出中文漢字 
            string str = gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[])));
            newRandom.Append(str);
     
        }
        return newRandom.ToString(); 
    }


    public static object[] CreateRegionCode(int strlength) 
        { 
            //定義一個字符串數組儲存漢字編碼的組成元素 
            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]; 
 
            /**//*每循環(huán)一次產生一個含兩個元素的十六進制字節(jié)數組,并將其放入bject數組中 
             每個漢字有四個區(qū)位碼組成 
             區(qū)位碼第1位和區(qū)位碼第2位作為字節(jié)數組第一個元素 
             區(qū)位碼第3位和區(qū)位碼第4位作為字節(jié)數組第二個元素 
            */ 
            for(int i=0;i<strlength;i ) 
            { 
                //區(qū)位碼第1位 
                int r1=rnd.Next(11,14); 
                string str_r1=rBase[r1].Trim(); 
 
                //區(qū)位碼第2位 
                rnd=new Random(r1*unchecked((int)DateTime.Now.Ticks) i);//更換隨機數發(fā)生器的種子避免產生重復值 
                int r2; 
                if (r1==13) 
                { 
                    r2=rnd.Next(0,7); 
                } 
                else 
                { 
                    r2=rnd.Next(0,16); 
                } 
                string str_r2=rBase[r2].Trim(); 
 
                //區(qū)位碼第3位 
                rnd=new Random(r2*unchecked((int)DateTime.Now.Ticks) i); 
                int r3=rnd.Next(10,16); 
                string str_r3=rBase[r3].Trim(); 
 
                //區(qū)位碼第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(); 
 
                //定義兩個字節(jié)變量存儲產生的隨機漢字區(qū)位碼 
                byte byte1=Convert.ToByte(str_r1   str_r2,16); 
                byte byte2=Convert.ToByte(str_r3   str_r4,16); 
                //將兩個字節(jié)變量存儲在字節(jié)數組中 
                byte[] str_r=new byte[]{byte1,byte2}; 
 
                //將產生的一個漢字的字節(jié)數組放入object數組中 
                bytes.SetValue(str_r,i); 
                 
            } 
 
            return bytes; 
 
            }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 汪清县| 合山市| 和田县| 扶风县| 承德市| 城口县| 林甸县| 黄石市| 太白县| 万源市| 巴青县| 长垣县| 纳雍县| 文成县| 西城区| 玉溪市| 宁河县| 清丰县| 页游| 德阳市| 澎湖县| 宁德市| 南昌市| 新蔡县| 潮州市| 庆阳市| 安龙县| 城口县| 正宁县| 安塞县| 都安| 老河口市| 乳山市| 永泰县| 宁陵县| 阿尔山市| 札达县| 固阳县| 兖州市| 驻马店市| 商水县|