diff --git a/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt b/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt index b1a8d9ad418..707925068dc 100644 --- a/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt +++ b/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt @@ -5145,10 +5145,6 @@ namespace Akka.Util.Internal.Collections public T Next() { } public System.Collections.Generic.IEnumerable ToVector() { } } - public class static ListExtensions - { - public static System.Collections.Generic.List Shuffle(this System.Collections.Generic.List @this) { } - } } namespace Akka.Util.Reflection { diff --git a/src/core/Akka.Cluster/ClusterDaemon.cs b/src/core/Akka.Cluster/ClusterDaemon.cs index ddcfbd3a520..7fc5b09595a 100644 --- a/src/core/Akka.Cluster/ClusterDaemon.cs +++ b/src/core/Akka.Cluster/ClusterDaemon.cs @@ -1863,7 +1863,10 @@ private void SendGossipRandom(int n) if (!IsSingletonCluster && n > 0) { var localGossip = _latestGossip; - var possibleTargets = new List(localGossip.Members.Where(m => ValidNodeForGossip(m.UniqueAddress)).Select(m => m.UniqueAddress)); + var possibleTargets = + localGossip.Members.Where(m => ValidNodeForGossip(m.UniqueAddress)) + .Select(m => m.UniqueAddress) + .ToList(); var randomTargets = possibleTargets.Count <= n ? possibleTargets : possibleTargets.Shuffle().Slice(0, n); randomTargets.ForEach(GossipTo); } diff --git a/src/core/Akka/Util/Internal/Collections/ListExtensions.cs b/src/core/Akka/Util/Internal/Collections/ListExtensions.cs index 8ab9f6dc9a4..55fc6136779 100644 --- a/src/core/Akka/Util/Internal/Collections/ListExtensions.cs +++ b/src/core/Akka/Util/Internal/Collections/ListExtensions.cs @@ -10,20 +10,19 @@ namespace Akka.Util.Internal.Collections { - public static class ListExtensions + internal static class ListExtensions { public static List Shuffle(this List @this) { var list = new List(@this); - var rng = new Random(); - var n = list.Count; - while (n > 1) + var r = ThreadLocalRandom.Current; + for (int i = list.Count - 1; i > 0; i--) { - n--; - var k = rng.Next(n + 1); - var value = list[k]; - list[k] = list[n]; - list[n] = value; + int index = r.Next(i); + //swap + var tmp = list[index]; + list[index] = list[i]; + list[i] = tmp; } return list; }