diff options
| author | dkanada <dkanada@users.noreply.github.com> | 2019-12-10 22:28:44 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-10 22:28:44 +0900 |
| commit | dee3076eaf331a8e3e4b220c34491f713914fbc0 (patch) | |
| tree | 0f604975ad10de0f6e66234c5f1a36a21c113ea8 /MediaBrowser.Common/Extensions/ShuffleExtensions.cs | |
| parent | b5d0bd0d764982c8df2c341c1f38c4d0636409c3 (diff) | |
| parent | a2c35e6dba02f068a3f06e5a4e4964e6539069d1 (diff) | |
Merge pull request #1923 from Bond-009/random
Don't shuffle some types by default
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; + } + } + } +} |
