-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Diagnostics; | ||
|
||
namespace Vivelin; | ||
|
||
public static class EnumerableExtensions | ||
{ | ||
public static T Sample<T>(this IReadOnlyList<T> source) | ||
=> source.Sample(Random.Shared); | ||
|
||
public static T Sample<T>(this IReadOnlyList<T> source, Random rng) | ||
{ | ||
var index = rng.Next(source.Count); | ||
return source[index]; | ||
} | ||
|
||
public static T WeightedSample<T>(this IEnumerable<T> source) where T : IWeighted | ||
=> source.WeightedSample(Random.Shared); | ||
|
||
public static T WeightedSample<T>(this IEnumerable<T> source, Random rng) where T : IWeighted | ||
{ | ||
var totalWeight = source.Sum(x => x.Weight); | ||
var targetWeight = rng.NextDouble() * totalWeight; | ||
|
||
foreach (var item in source) | ||
{ | ||
if (targetWeight < item.Weight) | ||
return item; | ||
|
||
targetWeight -= item.Weight; | ||
} | ||
|
||
throw new UnreachableException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Vivelin; | ||
|
||
public interface IWeighted | ||
{ | ||
double Weight { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Vivelin; | ||
|
||
public readonly record struct Weighted<T>(T Value, double Weight) : IWeighted | ||
{ | ||
public Weighted(T value) | ||
: this(value, 1.0) { } | ||
|
||
public static implicit operator T(Weighted<T> weighted) | ||
=> weighted.Value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace Vivelin.Core.Tests; | ||
|
||
public class EnumerableExtensionsTests | ||
{ | ||
[Fact] | ||
public void Sample_ReturnsRandomValue() | ||
{ | ||
var values = new List<string>(["Bulbasaur", "Charmander", "Squirtle"]); | ||
|
||
values.Sample(Random.Shared).Should().BeOneOf(values); | ||
} | ||
|
||
[Fact] | ||
public void Sample_ReturnsWeightedRandomValue() | ||
{ | ||
var values = new List<Weighted<string>>([ | ||
new("Bulbasaur", 100), | ||
new("Charmander", 1), | ||
new("Squirtle", 1), | ||
]); | ||
|
||
var results = Enumerable.Range(0, 100).Select(_ => values.WeightedSample(Random.Shared)); | ||
var bulbasaurs = results.Count(x => x.Value == "Bulbasaur"); | ||
var charmanders = results.Count(x => x.Value == "Charmander"); | ||
var squirtles = results.Count(x => x.Value == "Squirtle"); | ||
bulbasaurs.Should().NotBeCloseTo(charmanders, delta: 70); | ||
bulbasaurs.Should().NotBeCloseTo(squirtles, delta: 70); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters