diff --git a/BenchmarkDotNet.config b/BenchmarkDotNet.config new file mode 100644 index 0000000..e69de29 diff --git a/src/GameUtils/Extensions/CollectionExtensions.cs b/src/GameUtils/Extensions/CollectionExtensions.cs index 7b76057..319315a 100644 --- a/src/GameUtils/Extensions/CollectionExtensions.cs +++ b/src/GameUtils/Extensions/CollectionExtensions.cs @@ -236,38 +236,37 @@ public static T WeightedRandom(this IEnumerable source, Func wei return list[count - 1]; } - var itemsTotalWeight = 0f; - var hasElements = false; - foreach (var item in source) + var array = source.ToArray(); + int arrayCount = array.Length; + if (arrayCount == 0) { - hasElements = true; - itemsTotalWeight += weightSelector(item); + throw new InvalidOperationException("Sequence contains no elements."); } - if (!hasElements) + var arrayTotalWeight = 0f; + for (int i = 0; i < arrayCount; i++) { - throw new InvalidOperationException("Sequence contains no elements."); + arrayTotalWeight += weightSelector(array[i]); } - if (itemsTotalWeight <= 0) + if (arrayTotalWeight <= 0) { throw new InvalidOperationException("Total weight must be greater than zero."); } - var itemsTarget = (float)(Random.Shared.NextDouble() * itemsTotalWeight); - var itemsCumulative = 0f; - T lastItem = default!; + var arrayTarget = (float)(Random.Shared.NextDouble() * arrayTotalWeight); + var arrayCumulative = 0f; - foreach (var item in source) + for (int i = 0; i < arrayCount; i++) { - lastItem = item; - itemsCumulative += weightSelector(item); - if (itemsTarget <= itemsCumulative) + var item = array[i]; + arrayCumulative += weightSelector(item); + if (arrayTarget <= arrayCumulative) { return item; } } - return lastItem; + return array[arrayCount - 1]; } }