随机数组数据、随机int数字、随机字符串代码
#region 随机业务 /// <summary> /// 对该业务进行随机排序 // /// 08-10-24 /// </summary> /// <param name="list"></param> /// <returns></returns> private static List<Maticsoft.Model.TB_Ap> GetRandomSchemeList(List<Maticsoft.Model.TB_Ap> list) { List<Maticsoft.Model.TB_Ap> 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; } /// <summary> /// 随机数 /// </summary> public static Random random = new Random(); /// <summary> /// 随机不重复的Int 数值 /// </summary> /// <param name="minValue"></param> /// <param name="maxValue"></param> /// <param name="count"></param> /// <returns></returns> 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; }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索c#
, 数组
, 字符串
, int
, 数据
model
,以便于您获取更多的相关知识。
时间: 2024-10-01 23:41:39