随机数组数据、随机int数字、随机字符串代码
#region 随机业务 ////// 对该业务进行随机排序 // /// 08-10-24 /// /// ///private static List GetRandomSchemeList(List list) { List BuList = list; Maticsoft.Model.TB_Ap[] BuListtemp = new Maticsoft.Model.TB_Ap[list.Count]; int[] temp = GetRandomUnrepeatArray(0, BuList.Count - 1, BuList.Count); for (int i = 0; i < temp.Length; i++) { BuListtemp[i] = BuList[temp[i]]; } BuList.Clear(); BuList.AddRange(BuListtemp); return BuList; } /// /// 随机数 /// public static Random random = new Random(); ////// 随机不重复的Int 数值 /// /// /// /// ///public static int[] GetRandomUnrepeatArray(int minValue, int maxValue, int count) { //Random rnd = new Random(); int length = maxValue - minValue + 1; byte[] keys = new byte[length]; random.NextBytes(keys); int[] items = new int[length]; for (int i = 0; i < length; i++) { items[i] = i + minValue; } Array.Sort(keys, items); return items; } #endregion
例子2
private static int rep = 0; // /// 生成随机数字字符串 /// /// 待生成的位数 /// 生成的数字字符串 public static string GenerateCheckCodeNum(int codeCount) { string str = string.Empty; long num2 = DateTime.Now.Ticks + rep; rep++; Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep))); for (int i = 0; i < codeCount; i++) { int num = random.Next(); str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString(); } return str; } /// 生成随机字母字符串(数字字母混和) /// /// 待生成的位数 /// 生成的字母字符串 public static string GenerateCheckCode(int codeCount) { string str = string.Empty; long num2 = DateTime.Now.Ticks + rep; rep++; Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep))); for (int i = 0; i < codeCount; i++) { char ch; int num = random.Next(); if ((num % 2) == 0) { ch = (char)(0x30 + ((ushort)(num % 10))); } else { ch = (char)(0x41 + ((ushort)(num % 0x1a))); } str = str + ch.ToString(); } return str; }