/// <summary> /// 类说明:RandHelper类,用来实现取随机 /// 编码日期:2015-12-29 /// 编 码 人:里程碑 /// 修改日期:2015-12-29 /// 版 本 号:1.0 /// </summary> using System; using System.Collections.Generic; using System.Text; namespace DotNet.Utilities { /// <summary> /// 随机操作帮助类 /// </summary> public class RandHelper { private Random _random; public RandHelper() { //为随机数对象赋值 this._random = new Random(); } /// <summary> /// 随机范围数字 /// </summary> /// <param name="min">最小</param> /// <param name="max">最大</param> /// <returns></returns> public string RandNext(int min, int max) { return _random.Next(min, max).ToString(); } /// <summary> /// 返回一个介于0.0与1.0之间的随机数 /// </summary> /// <returns></returns> public string RandNextDouble() { return _random.NextDouble().ToString(); } /// <summary> /// 随机数字 /// </summary> /// <param name="len">长度</param> /// <returns></returns> public string RandNum(int len) { string str = string.Empty; ; i < len; i++) { str += _random.Next(, ); } return str; } /// <summary> /// 随机英文 /// </summary> /// <param name="Length">长度</param> /// <param name="type">类型,0=小写,1=大写,2=混合,默认小写</param> /// <returns></returns> ) { string[] s1 = { "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" }; //定义一个新字符串 string NewText = string.Empty; ; m < Length; m++) { NewText = NewText + s1[_random.Next(, s1.Length)]; } ) { NewText = NewText.ToLower(); } ) { NewText = NewText.ToUpper(); } return NewText; } /// <summary> /// 生成随机姓氏 /// </summary> /// <returns></returns> public string RandName() { /// <summary> /// 百家姓 /// </summary> string firstName = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯昝管卢莫柯房裘缪干解应宗丁宣贲邓郁单杭洪包诸左石崔吉钮龚程嵇邢滑裴陆荣翁荀羊于惠甄曲家封芮羿储靳汲邴糜松井段富巫乌焦巴弓牧隗山谷车侯宓蓬全郗班仰秋仲伊宫宁仇栾暴甘钭历戎祖武符刘景詹束龙叶幸司韶郜黎蓟溥印宿白怀蒲邰从鄂索咸籍赖卓蔺屠蒙池乔阳郁胥能苍双闻莘党翟谭贡劳逄姬申扶堵冉宰郦雍却璩桑桂濮牛寿通边扈燕冀浦尚农温别庄晏柴瞿阎充慕连茹习宦艾鱼容向古易慎戈廖庾终暨居衡步都耿满弘匡国文寇广禄阙东欧殳沃利蔚越夔隆师巩厍聂晁勾敖融冷訾辛阚那简饶空曾毋沙乜养鞠须丰巢关蒯相查后荆红游竺权逮盍益桓公"; , firstName.Length - ), )); } public string RandChinese(int strlength) { //定义一个字符串数组储存汉字编码的组成元素 ] { ", "a", "b", "c", "d", "e", "f" }; Random rnd = new Random(); //定义一个object数组用来 object[] bytes = new object[strlength]; /*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中 每个汉字有四个区位码组成 区位码第1位和区位码第2位作为字节数组第一个元素 区位码第3位和区位码第4位作为字节数组第二个元素 */ ; i < strlength; i++) { //区位码第1位 , ); string str_r1 = rBase[r1].Trim(); //区位码第2位 rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的 //种子避免产生重复值 int r2; ) { r2 = rnd.Next(, ); } else { r2 = rnd.Next(, ); } string str_r2 = rBase[r2].Trim(); //区位码第3位 rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i); , ); string str_r3 = rBase[r3].Trim(); //区位码第4位 rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i); int r4; ) { r4 = rnd.Next(, ); } ) { r4 = rnd.Next(, ); } else { r4 = rnd.Next(, ); } string str_r4 = rBase[r4].Trim(); //定义两个字节变量存储产生的随机汉字区位码 ); ); //将两个字节变量存储在字节数组中 byte[] str_r = new byte[] { byte1, byte2 }; //将产生的一个汉字的字节数组放入object数组中 bytes.SetValue(str_r, i); } //获取GB2312编码页(表) Encoding gb = Encoding.GetEncoding("gb2312"); string str1 = string.Empty; ; i < bytes.Length; i++) { str1 += gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[]))); } return str1; } } }