| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Linq; |
| 4 | using System.Text; |
| 5 | using System.Threading; |
| 6 | using System.Threading.Tasks; |
| 7 | |
| 8 | namespace 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 | /// |
| 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 | } |