Skip to content

Commit

Permalink
增加Shuffle方法
Browse files Browse the repository at this point in the history
  • Loading branch information
j4587698 committed Jun 7, 2024
1 parent 1db0fdb commit f130478
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
25 changes: 25 additions & 0 deletions Jx.Toolbox/Extensions/ListExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;

namespace Jx.Toolbox.Extensions
{
public static class ListExtension
{
/// <summary>
/// 简易洗牌算法,支持IList
/// </summary>
/// <param name="list"></param>
/// <typeparam name="T"></typeparam>
public static void Shuffle<T>(this IList<T> 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]);
}
}
}
}
2 changes: 1 addition & 1 deletion Jx.Toolbox/Jx.Toolbox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageProjectUrl>https://github.com/j4587698/Jx.Toolbox</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>tool</PackageTags>
<PackageVersion>0.3.11</PackageVersion>
<PackageVersion>0.3.12</PackageVersion>
</PropertyGroup>

</Project>
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ ByteArrayExtension
```
byte[].ToHexString(); // 将byte数组转换为16进制字符串
```

ListExtension
```
IList.Shuffle(); //对指定的IList进行洗牌
```

## Hash库
```
MD5.MD5String("要计算得字符串"); // 获取32位小写的MD5串
Expand Down

0 comments on commit f130478

Please sign in to comment.