phase 2
This commit is contained in:
@@ -25,8 +25,14 @@ namespace EscapeRoomEngine.Engine.Runtime.Utilities
|
||||
|
||||
public static int RandomInclusive(int from, int to) => Random.Range(from, to + 1);
|
||||
|
||||
public static T RandomElement<T>(this List<T> list) => list[Random.Range(0, list.Count)];
|
||||
#endregion
|
||||
}
|
||||
|
||||
public static class ListExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// remove a random element from a list and return it.
|
||||
/// </summary>
|
||||
public static T PopRandomElement<T>(this List<T> list)
|
||||
{
|
||||
var index = Random.Range(0, list.Count);
|
||||
@@ -35,7 +41,20 @@ namespace EscapeRoomEngine.Engine.Runtime.Utilities
|
||||
return element;
|
||||
}
|
||||
|
||||
#endregion
|
||||
public static T RandomElement<T>(this List<T> list) => list[Random.Range(0, list.Count)];
|
||||
|
||||
/// <summary>
|
||||
/// Perform a Fisher-Yates shuffle on a given list, leaving it in a random order.
|
||||
/// </summary>
|
||||
public static void Shuffle<T>(this List<T> list)
|
||||
{
|
||||
for (var n = list.Count - 1; n > 0; n--)
|
||||
{
|
||||
var i = Utilities.RandomInclusive(0, n);
|
||||
var j = Utilities.RandomInclusive(0, n);
|
||||
(list[i], list[j]) = (list[j], list[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class Vector2IntExtensions
|
||||
|
||||
Reference in New Issue
Block a user