mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-25 14:08:34 +01:00
Allow RandomExtensions to operate on any ICollection
This commit is contained in:
parent
e673ccea61
commit
48dfa8f1ee
2 changed files with 9 additions and 7 deletions
|
@ -26,6 +26,7 @@ Improvements
|
|||
- Allow using multiple textures in a StaticSpriteBatch
|
||||
- Added GenericInput support for Buttons.None
|
||||
- Improved the way terminating formatting codes work by introducing SimpleEndCode
|
||||
- Allow RandomExtensions to operate on any ICollection
|
||||
|
||||
Removals
|
||||
- Marked AStar.InfiniteCost as obsolete
|
||||
|
|
|
@ -9,18 +9,19 @@ namespace MLEM.Extensions {
|
|||
public static class RandomExtensions {
|
||||
|
||||
/// <summary>
|
||||
/// Gets a random entry from the given list with uniform chance.
|
||||
/// Gets a random entry from the given collection with uniform chance.
|
||||
/// </summary>
|
||||
/// <param name="random">The random</param>
|
||||
/// <param name="entries">The entries to choose from</param>
|
||||
/// <typeparam name="T">The entries' type</typeparam>
|
||||
/// <returns>A random entry</returns>
|
||||
public static T GetRandomEntry<T>(this Random random, IList<T> entries) {
|
||||
return entries[random.Next(entries.Count)];
|
||||
public static T GetRandomEntry<T>(this Random random, ICollection<T> entries) {
|
||||
// ElementAt internally optimizes for IList access so we don't have to here
|
||||
return entries.ElementAt(random.Next(entries.Count));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random entry from the given list based on the specified weight function.
|
||||
/// Returns a random entry from the given collection based on the specified weight function.
|
||||
/// A higher weight for an entry increases its likeliness of being picked.
|
||||
/// </summary>
|
||||
/// <param name="random">The random</param>
|
||||
|
@ -29,7 +30,7 @@ namespace MLEM.Extensions {
|
|||
/// <typeparam name="T">The entries' type</typeparam>
|
||||
/// <returns>A random entry, based on the entries' weight</returns>
|
||||
/// <exception cref="IndexOutOfRangeException">If the weight function returns different weights for the same entry</exception>
|
||||
public static T GetRandomWeightedEntry<T>(this Random random, IList<T> entries, Func<T, int> weightFunc) {
|
||||
public static T GetRandomWeightedEntry<T>(this Random random, ICollection<T> entries, Func<T, int> weightFunc) {
|
||||
var totalWeight = entries.Sum(weightFunc);
|
||||
var goalWeight = random.Next(totalWeight);
|
||||
var currWeight = 0;
|
||||
|
@ -41,8 +42,8 @@ namespace MLEM.Extensions {
|
|||
throw new IndexOutOfRangeException();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="GetRandomWeightedEntry{T}(System.Random,System.Collections.Generic.IList{T},System.Func{T,int})"/>
|
||||
public static T GetRandomWeightedEntry<T>(this Random random, IList<T> entries, Func<T, float> weightFunc) {
|
||||
/// <inheritdoc cref="GetRandomWeightedEntry{T}(System.Random,System.Collections.Generic.ICollection{T},System.Func{T,int})"/>
|
||||
public static T GetRandomWeightedEntry<T>(this Random random, ICollection<T> entries, Func<T, float> weightFunc) {
|
||||
var totalWeight = entries.Sum(weightFunc);
|
||||
var goalWeight = random.NextDouble() * totalWeight;
|
||||
var currWeight = 0F;
|
||||
|
|
Loading…
Reference in a new issue