diff --git a/EasyTool.Core/RandomCategoty/RandomExtension.cs b/EasyTool.Core/RandomCategoty/RandomExtension.cs new file mode 100644 index 0000000..3779261 --- /dev/null +++ b/EasyTool.Core/RandomCategoty/RandomExtension.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.RandomCategoty +{ + public static class RandomExtension + { + + /// + /// 返回四位数的正整数字符串 + /// + /// + public static string FourNum() => RandomUtil.FourNum(); + + /// + /// 返回六位数的正整数字符串 + /// + /// + public static string SixNum() => RandomUtil.SixNum(); + /// + /// 返回位数区间内的数字字符串 + /// + /// 左闭右开 + /// + /// 最小位数 + /// 最大位数 + /// + /// <0,抛出异常 + public static string RandomNum(this Tuple tuple) => RandomUtil.RandomNum(tuple.Item1, tuple.Item2); + /// + /// 返回区间内的数字字 + /// + /// 左闭右开 + /// + /// 最小数 + /// 最大数 + /// + public static int RandomInt(this Tuple tuple) => RandomUtil.RandomInt(tuple.Item1, tuple.Item2); + /// + /// 返回区间内指定个数随机数 + /// 左闭右开 + /// + /// + /// + /// 输出数量 + /// + /// <0,抛出异常 + public static List RandomNum(this Tuple tuple) => RandomUtil.RandomNum(tuple.Item1, tuple.Item2, tuple.Item3); + + /// + /// 洗牌算法打乱List里面的顺序 + /// + /// + /// + public static void Shuffle(this IList list) => RandomUtil.Shuffle(list); + + + /// + /// 返回列表里面确定数量的随机元素 + /// + /// 源数据类型 + /// 源数据列表 + /// 需要返回数量 + /// 是否需要不重复的数据 + /// + /// + public static List GetRandomElements(this List sourceList, int needCount, bool needOnly = true) => RandomUtil.GetRandomElements(sourceList, needCount, needOnly); + + } +} diff --git a/EasyTool.Core/RandomCategoty/RandomUtil.cs b/EasyTool.Core/RandomCategoty/RandomUtil.cs new file mode 100644 index 0000000..1e5c68a --- /dev/null +++ b/EasyTool.Core/RandomCategoty/RandomUtil.cs @@ -0,0 +1,163 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.RandomCategoty +{ + public static class RandomUtil + { + private static readonly Random _random = new(); + /// + /// 返回四位数的正整数字符串 + /// + /// + public static string FourNum() + { + return _random.Next(1000, 10000).ToString(); + } + /// + /// 返回六位数的正整数字符串 + /// + /// + public static string SixNum() + { + return _random.Next(1000000, 10000000).ToString(); + } + /// + /// 返回位数区间内的数字字符串 + /// + /// 左闭右开 + /// + /// 最小位数 + /// 最大位数 + /// + /// <0,抛出异常 + public static string RandomNum(int minBit, int maxBit) + { + if (minBit < 0) + { + throw new NotSupportedException("minBit参数错误"); + } + int minValue = (int)Math.Pow(10, minBit); + int maxValue = (int)Math.Pow(10, maxBit); + return _random.Next(minValue, maxValue).ToString(); + } + /// + /// 返回区间内的数字字 + /// + /// 左闭右开 + /// + /// 最小数 + /// 最大数 + /// + public static int RandomInt(int minValue, int maxValue) + { + + return _random.Next(minValue, maxValue); + } + /// + /// 返回区间内指定个数随机数 + /// 左闭右开 + /// + /// + /// + /// 输出数量 + /// + /// <0,抛出异常 + public static List RandomNum(int minValue, int maxValue, int refCount) + { + var result = new List(); + if (refCount < 0) + { + throw new NotSupportedException("refCount参数错误"); + } + for (int i = 0; i < refCount; i++) + { + result.Add(_random.Next(minValue, maxValue)); + } + + return result; + } + /// + /// 洗牌算法打乱List里面的顺序 + /// + /// + /// + public static void Shuffle(this IList list) + { + int n = list.Count; + while (n > 1) + { + n--; + int k = _random.Next(n + 1); + (list[n], list[k]) = (list[k], list[n]); + } + } + + /// + /// 返回列表里面确定数量的随机元素 + /// + /// 源数据类型 + /// 源数据列表 + /// 需要返回数量 + /// 是否需要不重复的数据 + /// + /// + public static List GetRandomElements(this List sourceList, int needCount, bool needOnly = true) + { + if (needCount<=0) + { + throw new NotSupportedException("needCount参数错误"); + } + var length = sourceList.Count; + if (needOnly) + { + if (needCount >= length) + { + // 如果请求的元素数量大于等于列表的元素数量,返回整个列表 + return new List(sourceList); + } + + List resultList = new List(needCount); + + HashSet selectedIndices = new HashSet(); + + while (needCount-- > 0) + { + int randomIndex = _random.Next(0, length); + + // 如果该索引已经被选择过,则重新选择 + if (selectedIndices.Contains(randomIndex)) + { + continue; + } + + resultList.Add(sourceList[randomIndex]); + selectedIndices.Add(randomIndex); + } + + return resultList; + } + else + { + + if (length <= 1) + { + return sourceList; + } + List resultList = new List(needCount); + + for (int i = 0; i < needCount; i++) + { + int randomIndex = _random.Next(0, sourceList.Count); + resultList.Add(sourceList[randomIndex]); + } + + return resultList; + } + + + } + + } +}