From f130478d59470c2af635060f938a71c6dabb422a Mon Sep 17 00:00:00 2001 From: jx Date: Fri, 7 Jun 2024 21:55:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Shuffle=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Jx.Toolbox/Extensions/ListExtension.cs | 25 +++++++++++++++++++++++++ Jx.Toolbox/Jx.Toolbox.csproj | 2 +- README.md | 6 ++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 Jx.Toolbox/Extensions/ListExtension.cs diff --git a/Jx.Toolbox/Extensions/ListExtension.cs b/Jx.Toolbox/Extensions/ListExtension.cs new file mode 100644 index 0000000..ffd8376 --- /dev/null +++ b/Jx.Toolbox/Extensions/ListExtension.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; + +namespace Jx.Toolbox.Extensions +{ + public static class ListExtension + { + /// + /// 简易洗牌算法,支持IList + /// + /// + /// + public static void Shuffle(this IList list) + { + var rng = new Random(); + int n = list.Count; + while (n > 1) + { + n--; + int k = rng.Next(n + 1); + (list[k], list[n]) = (list[n], list[k]); + } + } + } +} \ No newline at end of file diff --git a/Jx.Toolbox/Jx.Toolbox.csproj b/Jx.Toolbox/Jx.Toolbox.csproj index 5bf4c10..7eee477 100644 --- a/Jx.Toolbox/Jx.Toolbox.csproj +++ b/Jx.Toolbox/Jx.Toolbox.csproj @@ -9,7 +9,7 @@ https://github.com/j4587698/Jx.Toolbox MIT tool - 0.3.11 + 0.3.12 diff --git a/README.md b/README.md index 3af6e30..b0ad55a 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,12 @@ ByteArrayExtension ``` byte[].ToHexString(); // 将byte数组转换为16进制字符串 ``` + +ListExtension +``` +IList.Shuffle(); //对指定的IList进行洗牌 +``` + ## Hash库 ``` MD5.MD5String("要计算得字符串"); // 获取32位小写的MD5串