跳到主要内容

github/tai

公开

Tai · Windows 软件与网站使用时长统计。上游 Planshit/Tai 的源码镜像,非灵栈原创;MIT 许可。来源:https://github.com/Planshit/Tai 。本次为手动同步,暂不提供安装包。

浏览文件

ListExtensions.cs

39 行 · 1.0 KB历史下载
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading;
6using System.Threading.Tasks;
7
8namespace UI.Extensions
9{
10 public static class ThreadSafeRandom
11 {
12 [ThreadStatic] private static Random Local;
13
14 public static Random ThisThreadsRandom
15 {
16 get { return Local ?? (Local = new Random(unchecked(Environment.TickCount * 31 + Thread.CurrentThread.ManagedThreadId))); }
17 }
18 }
19 public static class ListExtensions
20 {
21 /// <summary>
22 /// 随机打乱该List
23 /// </summary>
24 /// <typeparam name="T"></typeparam>
25 /// <param name="list"></param>
26 public static void Shuffle<T>(this IList<T> list)
27 {
28 int n = list.Count;
29 while (n > 1)
30 {
31 n--;
32 int k = ThreadSafeRandom.ThisThreadsRandom.Next(n + 1);
33 T value = list[k];
34 list[k] = list[n];
35 list[n] = value;
36 }
37 }
38 }
39}