使用c#生成随机数总结

时间:2021-05-02

privatestaticchar[]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="得到数字加英文的随机数,参数:随机数长度,返回值:一个字符串!")]publicstringpxkt_GetCharFont(intstrLength){System.Text.StringBuildernewRandom=newSystem.Text.StringBuilder(62);Randomrd=newRandom();for(inti=0;i<strLength;i ){newRandom.Append(constant[rd.Next(62)]);}returnnewRandom.ToString();}privatestaticchar[]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="得到英文的随机数,参数:随机数长度,返回值:一个字符串!")]publicstringGetEnglishChar(intstrLength){System.Text.StringBuildernewRandom=newSystem.Text.StringBuilder(52);Randomrd=newRandom();for(inti=0;i<strLength;i ){newRandom.Append(englishchar[rd.Next(52)]);}returnnewRandom.ToString();}privatestaticchar[]numchar={'0','1','2','3','4','5','6','7','8','9'};[WebMethod(Description="得到数字的随机数,参数:随机数长度,返回值:一个字符串!")]publicstringGetNumChar(intstrLength){System.Text.StringBuildernewRandom=newSystem.Text.StringBuilder(10);Randomrd=newRandom();for(inti=0;i<strLength;i ){newRandom.Append(numchar[rd.Next(10)]);}returnnewRandom.ToString();}[WebMethod(Description="得到中文的随机数,参数:随机数长度,返回值:一个字符串!")]publicstringGetChineseChar(intstrLength){System.Text.StringBuildernewRandom=newSystem.Text.StringBuilder();//获取GB2312编码页(表)System.Text.Encodinggb=System.Text.Encoding.GetEncoding("gb2312");//调用函数产生I个随机中文汉字编码object[]bytes=CreateRegionCode(strLength);for(inti=0;i<strLength;i ){//根据汉字编码的字节数组解码出中文汉字stringstr=gb.GetString((byte[])Convert.ChangeType(bytes[i],typeof(byte[])));newRandom.Append(str);}returnnewRandom.ToString();}publicstaticobject[]CreateRegionCode(intstrlength){//定义一个字符串数组储存汉字编码的组成元素string[]rBase=newString[16]{"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};Randomrnd=newRandom();//定义一个object数组用来object[]bytes=newobject[strlength];for(inti=0;i<strlength;i ){//区位码第1位intr1=rnd.Next(11,14);stringstr_r1=rBase[r1].Trim();//区位码第2位rnd=newRandom(r1*unchecked((int)DateTime.Now.Ticks) i);//更换随机数发生器的种子避免产生重复值intr2;if(r1==13){r2=rnd.Next(0,7);}else{r2=rnd.Next(0,16);}stringstr_r2=rBase[r2].Trim();//区位码第3位rnd=newRandom(r2*unchecked((int)DateTime.Now.Ticks) i);intr3=rnd.Next(10,16);stringstr_r3=rBase[r3].Trim();//区位码第4位rnd=newRandom(r3*unchecked((int)DateTime.Now.Ticks) i);intr4;if(r3==10){r4=rnd.Next(1,16);}elseif(r3==15){r4=rnd.Next(0,15);}else{r4=rnd.Next(0,16);}stringstr_r4=rBase[r4].Trim();//定义两个字节变量存储产生的随机汉字区位码bytebyte1=Convert.ToByte(str_r1 str_r2,16);bytebyte2=Convert.ToByte(str_r3 str_r4,16);//将两个字节变量存储在字节数组中byte[]str_r=newbyte[]{byte1,byte2};//将产生的一个汉字的字节数组放入object数组中bytes.SetValue(str_r,i);}returnbytes;}

原文地址:http://lanustudio.bokee.com/viewdiary.25518143.html

本文源自:翔宇亭——IT乐园(http://),转载请保留此信息!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章