From d06b911d12a4845c30e478e2b55b73b5eaaea793 Mon Sep 17 00:00:00 2001 From: Sheldon-NULL <83170675+Sheldon-NULL@users.noreply.github.com> Date: Wed, 13 Dec 2023 10:11:23 +0800 Subject: [PATCH 1/2] Add RandomHelper --- .../RandomCategoty/RandomExtension.cs | 33 ++++++++ EasyTool.Core/RandomCategoty/RandomUtil.cs | 75 +++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 EasyTool.Core/RandomCategoty/RandomExtension.cs create mode 100644 EasyTool.Core/RandomCategoty/RandomUtil.cs diff --git a/EasyTool.Core/RandomCategoty/RandomExtension.cs b/EasyTool.Core/RandomCategoty/RandomExtension.cs new file mode 100644 index 0000000..71afa0f --- /dev/null +++ b/EasyTool.Core/RandomCategoty/RandomExtension.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace EasyTool.RandomCategoty +{ + public static class RandomExtension + { + /// + /// 返回位数区间内的数字字符串 + /// + /// 左闭右开 + /// + /// 最小位数 + /// 最大位数 + /// + /// <=0,抛出异常 + /// <=0,抛出异常 + public static string RandomNum(this Tuple tuple) => RandomUtil.RandomNum(tuple.Item1, tuple.Item2); + + /// + /// 返回区间内指定个数随机数 + /// 左闭右开 + /// + /// + /// + /// 输出数量 + /// + /// <0,抛出异常 + public static List RandomNum(this Tuple tuple) => RandomUtil.RandomNum(tuple.Item1, tuple.Item2,tuple.Item3); + + } +} diff --git a/EasyTool.Core/RandomCategoty/RandomUtil.cs b/EasyTool.Core/RandomCategoty/RandomUtil.cs new file mode 100644 index 0000000..263860b --- /dev/null +++ b/EasyTool.Core/RandomCategoty/RandomUtil.cs @@ -0,0 +1,75 @@ +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,抛出异常 + /// <=0,抛出异常 + public static string RandomNum(int minBit, int maxBit) + { + if (minBit <= 0) + { + throw new NotSupportedException("minBit参数错误"); + } + if (maxBit <= 0) + { + throw new NotSupportedException("maxBit参数错误"); + } + int minValue = (int)Math.Pow(10, minBit); + int maxValue = (int)Math.Pow(10, maxBit); + return _random.Next(minValue, maxValue).ToString(); + } + + /// + /// 返回区间内指定个数随机数 + /// 左闭右开 + /// + /// + /// + /// 输出数量 + /// <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; + } + + } +} From 3ba890a34552e0f020a66f00633b1cf502a8d210 Mon Sep 17 00:00:00 2001 From: Sheldon-NULL <83170675+Sheldon-NULL@users.noreply.github.com> Date: Fri, 1 Mar 2024 14:05:41 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Add=20=E6=96=B0=E5=A2=9E=E5=AF=B9=E9=9B=86?= =?UTF-8?q?=E5=90=88=E7=9A=84=E9=9A=8F=E6=9C=BA=E6=96=B9=E6=B3=95=E6=8B=93?= =?UTF-8?q?=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RandomCategoty/RandomExtension.cs | 46 +++++++- EasyTool.Core/RandomCategoty/RandomUtil.cs | 102 ++++++++++++++++-- 2 files changed, 137 insertions(+), 11 deletions(-) diff --git a/EasyTool.Core/RandomCategoty/RandomExtension.cs b/EasyTool.Core/RandomCategoty/RandomExtension.cs index 71afa0f..3779261 100644 --- a/EasyTool.Core/RandomCategoty/RandomExtension.cs +++ b/EasyTool.Core/RandomCategoty/RandomExtension.cs @@ -6,6 +6,18 @@ namespace EasyTool.RandomCategoty { public static class RandomExtension { + + /// + /// 返回四位数的正整数字符串 + /// + /// + public static string FourNum() => RandomUtil.FourNum(); + + /// + /// 返回六位数的正整数字符串 + /// + /// + public static string SixNum() => RandomUtil.SixNum(); /// /// 返回位数区间内的数字字符串 /// @@ -14,10 +26,17 @@ public static class RandomExtension /// 最小位数 /// 最大位数 /// - /// <=0,抛出异常 - /// <=0,抛出异常 + /// <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); /// /// 返回区间内指定个数随机数 /// 左闭右开 @@ -27,7 +46,26 @@ public static class RandomExtension /// 输出数量 /// /// <0,抛出异常 - public static List RandomNum(this Tuple tuple) => RandomUtil.RandomNum(tuple.Item1, tuple.Item2,tuple.Item3); + 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 index 263860b..1e5c68a 100644 --- a/EasyTool.Core/RandomCategoty/RandomUtil.cs +++ b/EasyTool.Core/RandomCategoty/RandomUtil.cs @@ -31,23 +31,30 @@ public static string SixNum() /// 最小位数 /// 最大位数 /// - /// <=0,抛出异常 - /// <=0,抛出异常 + /// <0,抛出异常 public static string RandomNum(int minBit, int maxBit) { - if (minBit <= 0) + if (minBit < 0) { throw new NotSupportedException("minBit参数错误"); } - if (maxBit <= 0) - { - throw new NotSupportedException("maxBit参数错误"); - } 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); + } /// /// 返回区间内指定个数随机数 /// 左闭右开 @@ -55,6 +62,7 @@ public static string RandomNum(int minBit, int maxBit) /// /// /// 输出数量 + /// /// <0,抛出异常 public static List RandomNum(int minValue, int maxValue, int refCount) { @@ -70,6 +78,86 @@ public static List RandomNum(int minValue, int maxValue, int refCount) 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; + } + + + } } }