diff options
| author | dkanada <dkanada@users.noreply.github.com> | 2020-01-08 01:21:09 +0900 |
|---|---|---|
| committer | dkanada <dkanada@users.noreply.github.com> | 2020-01-08 01:21:09 +0900 |
| commit | aca31457c06ea13042accd60e27ab61208a51577 (patch) | |
| tree | b734310d099f9b896ccce0b200ab96a3786d168b /MediaBrowser.Common/Extensions/ShuffleExtensions.cs | |
| parent | dee247453e7b5cab1badb6a844af690cdf80aacd (diff) | |
| parent | 0b592376d59d10d14dbdd248c24f7ec6397c3508 (diff) | |
merge branch master into media-attachments
Diffstat (limited to 'MediaBrowser.Common/Extensions/ShuffleExtensions.cs')
| -rw-r--r-- | MediaBrowser.Common/Extensions/ShuffleExtensions.cs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/MediaBrowser.Common/Extensions/ShuffleExtensions.cs b/MediaBrowser.Common/Extensions/ShuffleExtensions.cs new file mode 100644 index 000000000..5889d09c4 --- /dev/null +++ b/MediaBrowser.Common/Extensions/ShuffleExtensions.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; + +namespace MediaBrowser.Common.Extensions +{ + /// <summary> + /// Provides <c>Shuffle</c> extensions methods for <see cref="IList{T}" />. + /// </summary> + public static class ShuffleExtensions + { + private static readonly Random _rng = new Random(); + + /// <summary> + /// Shuffles the items in a list. + /// </summary> + /// <param name="list">The list that should get shuffled.</param> + /// <typeparam name="T">The type.</typeparam> + public static void Shuffle<T>(this IList<T> list) + { + int n = list.Count; + while (n > 1) + { + n--; + int k = _rng.Next(n + 1); + T value = list[k]; + list[k] = list[n]; + list[n] = value; + } + } + } +} |
