Skip to content

Commit

Permalink
Added SelectRandomElements extension
Browse files Browse the repository at this point in the history
Small fix
  • Loading branch information
bttf92 committed Nov 17, 2021
1 parent 99c6002 commit def8138
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 28 additions & 0 deletions Extensions/OtherExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,33 @@ public static bool Implements<T>(this object obj)
{
return obj.GetType().GetInterfaces().Contains(typeof(T));
}

/// <summary>
/// Returns a list with <paramref name="count"/> random elements from <paramref name="sequence"/>.
/// </summary>
/// <typeparam name="T"><see cref="Type"/> of the element.</typeparam>
/// <param name="sequence"><see cref="IEnumerable{T}"/></param>
/// <param name="count">Number of random elements.</param>
/// <returns>List of random elements.</returns>
public static List<T> SelectRandomElements<T>(this IEnumerable<T> sequence, int count) where T : class
{
List<T> ret = new List<T>();

if (count >= sequence.Count())
return sequence.ToList();

if (count < 1)
return null;

while(ret.Count < count)
{
var select = sequence.SelectRandomElement();

if (!ret.Contains(select))
ret.Add(select);
}

return ret;
}
}
}
2 changes: 1 addition & 1 deletion FusionLibrary.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>FusionLibrary.SHVDN3</id>
<version>1.2.0</version>
<version>1.2.2</version>
<authors>MrFusion92</authors>
<requireLicenseAcceptance>true</requireLicenseAcceptance>
<license type="expression">MIT</license>
Expand Down

0 comments on commit def8138

Please sign in to comment.