mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 20:58:34 +01:00
Added float version of GetRandomWeightedEntry
This commit is contained in:
parent
c360c90f28
commit
af7c341d83
2 changed files with 14 additions and 0 deletions
|
@ -18,6 +18,7 @@ Additions
|
||||||
Improvements
|
Improvements
|
||||||
- Generify GenericFont's string drawing
|
- Generify GenericFont's string drawing
|
||||||
- Added InputHandler mouse and touch position querying that preserves the game's viewport
|
- Added InputHandler mouse and touch position querying that preserves the game's viewport
|
||||||
|
- Added float version of GetRandomWeightedEntry
|
||||||
|
|
||||||
Fixes
|
Fixes
|
||||||
- **Fixed a formatting Code only knowing about the last Token that it is applied in**
|
- **Fixed a formatting Code only knowing about the last Token that it is applied in**
|
||||||
|
|
|
@ -41,5 +41,18 @@ namespace MLEM.Extensions {
|
||||||
throw new IndexOutOfRangeException();
|
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) {
|
||||||
|
var totalWeight = entries.Sum(weightFunc);
|
||||||
|
var goalWeight = random.NextDouble() * totalWeight;
|
||||||
|
var currWeight = 0F;
|
||||||
|
foreach (var entry in entries) {
|
||||||
|
currWeight += weightFunc(entry);
|
||||||
|
if (currWeight >= goalWeight)
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
throw new IndexOutOfRangeException();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue