Skip to content

Commit

Permalink
fix: Fixing issue with pulling a randomized subset of a list and the …
Browse files Browse the repository at this point in the history
…list is empty

1. The "BigBook" package in the Mirage.csproj file was updated from version 5.1.15 to 5.1.19.

2. The "Mecha.xUnit" package in the Mirage.Tests.csproj file was updated from version 2.1.39 to 2.1.40.

3. Two new conditions were added to the "Next" methods in the Random.cs file. These conditions check the state of a list and return the list if it is empty or if the count of the list is not equal to the count of the weights.
  • Loading branch information
JaCraig committed Mar 7, 2024
1 parent 98a938f commit e649fd9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Mirage/Mirage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="BigBook" Version="5.1.15" />
<PackageReference Include="BigBook" Version="5.1.19" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
Expand Down
15 changes: 9 additions & 6 deletions src/Mirage/Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ public Random(int seed)
GeneratorBuilder = Services.ServiceProvider?.GetService<Manager.Builder>() ?? new Manager.Builder(Array.Empty<IGenerator>());
}

/// <summary>
/// Gets or sets the generator builder.
/// </summary>
/// <value>The generator builder.</value>
private Manager.Builder GeneratorBuilder { get; }

/// <summary>
/// The local seed
/// </summary>
Expand All @@ -65,6 +59,12 @@ public Random(int seed)
/// </summary>
private readonly System.Random _GlobalSeed = new();

/// <summary>
/// Gets or sets the generator builder.
/// </summary>
/// <value>The generator builder.</value>
private Manager.Builder GeneratorBuilder { get; }

/// <summary>
/// Randomly generates a value of the specified type
/// </summary>
Expand Down Expand Up @@ -174,6 +174,8 @@ public T Next<T>(IEnumerable<T> list)
public IEnumerable<T>? Next<T>(IEnumerable<T>? list, int count, bool unique = false)
{
list ??= Array.Empty<T>();
if (!list.Any())
return list;
if (count >= list.Count() && unique)
return Shuffle(list);
var ReturnValue = new List<T>();
Expand Down Expand Up @@ -211,6 +213,7 @@ public T Next<T>(IEnumerable<T> list)
{
list ??= Array.Empty<T>();
weights ??= Array.Empty<decimal>();
if (!list.Any() || list.Count() != weights.Count()) return list;
if (count >= list.Count() && unique)
return Shuffle(list);
var ReturnValue = new List<T>();
Expand Down
2 changes: 1 addition & 1 deletion test/Mirage.Tests/Mirage.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Mecha.xUnit" Version="2.1.39" />
<PackageReference Include="Mecha.xUnit" Version="2.1.40" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Valkyrie" Version="4.0.60" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
Expand Down

0 comments on commit e649fd9

Please sign in to comment.