2022-06-18 11:04:24 -07:00
|
|
|
using System;
|
2016-07-09 15:30:12 -07:00
|
|
|
|
2022-06-18 11:04:24 -07:00
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
public static partial class Util
|
2016-07-09 15:30:12 -07:00
|
|
|
{
|
2023-01-21 20:02:33 -08:00
|
|
|
public static Random Rand => Random.Shared;
|
2020-01-25 21:49:52 -08:00
|
|
|
|
2022-06-18 11:04:24 -07: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-14 20:58:55 -08:00
|
|
|
|
2022-06-18 11:04:24 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Shuffles the order of items within a collection of items.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">Item type</typeparam>
|
|
|
|
/// <param name="items">Item collection</param>
|
|
|
|
public static void Shuffle<T>(Span<T> items) => Shuffle(items, 0, items.Length, Rand);
|
2019-06-08 19:56:11 -07:00
|
|
|
|
2022-06-18 11:04:24 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Shuffles the order of items within a collection of items.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">Item type</typeparam>
|
|
|
|
/// <param name="items">Item collection</param>
|
|
|
|
/// <param name="start">Starting position</param>
|
|
|
|
/// <param name="end">Ending position</param>
|
|
|
|
/// <param name="rnd">RNG object to use</param>
|
|
|
|
public static void Shuffle<T>(Span<T> items, int start, int end, Random rnd)
|
|
|
|
{
|
|
|
|
for (int i = start; i < end; i++)
|
2016-07-09 15:30:12 -07:00
|
|
|
{
|
2022-06-18 11:04:24 -07:00
|
|
|
int index = i + rnd.Next(end - i);
|
|
|
|
(items[index], items[i]) = (items[i], items[index]);
|
2016-07-09 15:30:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|