mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 17:58:26 -04:00
Updated some namespaces, finished some todos. v5 should be cancelled probably as the code is too intertwined to make it modular
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
namespace Nadeko.Common;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Nadeko.Common;
|
||||
|
||||
// made for expressions because they almost never get added
|
||||
// and they get looped through constantly
|
||||
@@ -48,4 +50,13 @@ public static class ArrayExtensions
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
public static T? RandomOrDefault<T>(this T[] data)
|
||||
{
|
||||
if (data.Length == 0)
|
||||
return default;
|
||||
|
||||
var index = RandomNumberGenerator.GetInt32(0, data.Length);
|
||||
return data[index];
|
||||
}
|
||||
}
|
@@ -38,8 +38,6 @@ public static class EnumerableExtensions
|
||||
public static string Join<T>(this IEnumerable<T> data, string separator, Func<T, string>? func = null)
|
||||
=> string.Join(separator, data.Select(func ?? (x => x?.ToString() ?? string.Empty)));
|
||||
|
||||
|
||||
// todo have 2 different shuffles
|
||||
/// <summary>
|
||||
/// Randomize element order by performing the Fisher-Yates shuffle
|
||||
/// </summary>
|
||||
@@ -47,21 +45,11 @@ public static class EnumerableExtensions
|
||||
/// <param name="items">Items to shuffle</param>
|
||||
public static IReadOnlyList<T> Shuffle<T>(this IEnumerable<T> items)
|
||||
{
|
||||
using var provider = RandomNumberGenerator.Create();
|
||||
var list = items.ToList();
|
||||
var n = list.Count;
|
||||
while (n > 1)
|
||||
var list = items.ToArray();
|
||||
var n = list.Length;
|
||||
while (n-- > 1)
|
||||
{
|
||||
var box = new byte[(n / byte.MaxValue) + 1];
|
||||
int boxSum;
|
||||
do
|
||||
{
|
||||
provider.GetBytes(box);
|
||||
boxSum = box.Sum(b => b);
|
||||
} while (!(boxSum < n * (byte.MaxValue * box.Length / n)));
|
||||
|
||||
var k = boxSum % n;
|
||||
n--;
|
||||
var k = RandomNumberGenerator.GetInt32(n);
|
||||
(list[k], list[n]) = (list[n], list[k]);
|
||||
}
|
||||
|
||||
|
@@ -3,7 +3,7 @@ using System.Security.Cryptography;
|
||||
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public class NadekoRandom : Random
|
||||
public sealed class NadekoRandom : Random
|
||||
{
|
||||
private readonly RandomNumberGenerator _rng;
|
||||
|
||||
@@ -16,27 +16,25 @@ public class NadekoRandom : Random
|
||||
_rng.GetBytes(bytes);
|
||||
return Math.Abs(BitConverter.ToInt32(bytes, 0));
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random integer between 0 (inclusive) and
|
||||
/// a specified exclusive upper bound using a cryptographically strong random number generator.
|
||||
/// </summary>
|
||||
/// <param name="maxValue">Exclusive max value</param>
|
||||
/// <returns>A random number</returns>
|
||||
public override int Next(int maxValue)
|
||||
{
|
||||
if (maxValue <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(maxValue));
|
||||
var bytes = new byte[sizeof(int)];
|
||||
_rng.GetBytes(bytes);
|
||||
return Math.Abs(BitConverter.ToInt32(bytes, 0)) % maxValue;
|
||||
}
|
||||
=> RandomNumberGenerator.GetInt32(maxValue);
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random integer between a specified inclusive lower bound and a
|
||||
/// specified exclusive upper bound using a cryptographically strong random number generator.
|
||||
/// </summary>
|
||||
/// <param name="minValue">Inclusive min value</param>
|
||||
/// <param name="maxValue">Exclusive max value</param>
|
||||
/// <returns>A random number</returns>
|
||||
public override int Next(int minValue, int maxValue)
|
||||
{
|
||||
if (minValue > maxValue)
|
||||
throw new ArgumentOutOfRangeException(nameof(maxValue));
|
||||
if (minValue == maxValue)
|
||||
return minValue;
|
||||
var bytes = new byte[sizeof(int)];
|
||||
_rng.GetBytes(bytes);
|
||||
var sign = Math.Sign(BitConverter.ToInt32(bytes, 0));
|
||||
return (sign * BitConverter.ToInt32(bytes, 0) % (maxValue - minValue)) + minValue;
|
||||
}
|
||||
=> RandomNumberGenerator.GetInt32(minValue, maxValue);
|
||||
|
||||
public long NextLong(long minValue, long maxValue)
|
||||
{
|
||||
|
Reference in New Issue
Block a user