2022-06-18 18:04:24 +00:00
|
|
|
using System;
|
2016-07-09 22:30:12 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
public static partial class Util
|
2016-07-09 22:30:12 +00:00
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
public static Random Rand => Random.Shared;
|
2020-01-26 05:49:52 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static uint Rand32() => Rand32(Rand);
|
|
|
|
public static uint Rand32(this Random rnd) => ((uint)rnd.Next(1 << 30) << 2) | (uint)rnd.Next(1 << 2);
|
|
|
|
public static ulong Rand64(this Random rnd) => rnd.Rand32() | ((ulong)rnd.Rand32() << 32);
|
2017-12-15 04:58:55 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Shuffles the order of items within a collection of items.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">Item type</typeparam>
|
|
|
|
/// <param name="rnd">RNG object to use</param>
|
2023-03-27 07:11:42 +00:00
|
|
|
/// <param name="items">Item collection</param>
|
|
|
|
public static void Shuffle<T>(this Random rnd, Span<T> items)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-03-27 07:11:42 +00:00
|
|
|
int n = items.Length;
|
|
|
|
for (int i = 0; i < n - 1; i++)
|
2016-07-09 22:30:12 +00:00
|
|
|
{
|
2023-03-27 07:11:42 +00:00
|
|
|
int j = rnd.Next(i, n);
|
|
|
|
if (j != i)
|
|
|
|
(items[i], items[j]) = (items[j], items[i]);
|
2016-07-09 22:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|