Fixes RNG (#181)

This commit is contained in:
Kamron Batman 2020-07-25 15:57:32 -07:00 committed by GitHub
parent 319a990ede
commit ff3a28ab84
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 273 additions and 115 deletions

View file

@ -1,42 +0,0 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: RandomProviders.cs - Created: 2020/05/24 - Updated: 2020/05/24 *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
namespace Server
{
public interface IRandomProvider
{
public ulong Next(ulong max);
public uint Next(uint max);
public bool NextBool();
public void GetBytes(Span<byte> b);
public double NextDouble();
}
public static class RandomProviders
{
private static IRandomProvider m_Provider;
private static IRandomProvider m_SecureProvider;
public static IRandomProvider Provider => m_Provider ??= new Xoshiro256PlusPlus();
public static IRandomProvider SecureProvider => m_SecureProvider ??= new SecureRandom();
}
}

View file

@ -1,42 +0,0 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: Random.cs - Created: 2019/12/30 - Updated: 2020/05/23 *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Security.Cryptography;
namespace Server
{
public class SecureRandom : RandomNumberGenerator, IRandomProvider
{
private readonly RandomNumberGenerator m_Random = new RNGCryptoServiceProvider();
public override void GetBytes(byte[] data) => m_Random.GetBytes(data);
public override void GetBytes(Span<byte> data) => m_Random.GetBytes(data);
public ulong Next(ulong c) => throw new NotImplementedException();
public uint Next(uint c) => throw new NotImplementedException();
public bool NextBool() => throw new NotImplementedException();
public double NextDouble() => throw new NotImplementedException();
}
}

View file

@ -28,6 +28,7 @@ using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Xml;
using Server.Random;
namespace Server
{
@ -854,7 +855,7 @@ namespace Server
var total = 0;
for (var i = 0; i < amount; ++i)
total += (int)RandomProviders.Provider.Next(sides) + 1;
total += (int)RandomSources.Source.Next(1, sides);
return total + bonus;
}
@ -864,19 +865,23 @@ namespace Server
var count = list.Count;
for (var i = count - 1; i > 0; i--)
{
var r = (int)RandomProviders.Provider.Next((uint)count);
var r = RandomSources.Source.Next(count);
var swap = list[r];
list[r] = list[i];
list[i] = swap;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int RandomList(params int[] list) => RandomList<int>(list);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T RandomList<T>(IList<T> list) => list[Random(list.Count)];
public static bool RandomBool() => RandomProviders.Provider.NextBool();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool RandomBool() => RandomSources.Source.NextBool();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int RandomMinMax(int min, int max)
{
if (min > max)
@ -890,27 +895,23 @@ namespace Server
return min;
}
return min + (int)RandomProviders.Provider.Next((uint)(max - min + 1));
return min + (int)RandomSources.Source.Next((uint)(max - min + 1));
}
public static int Random(int from, int count)
{
if (count == 0) return from;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Random(int from, int count) => RandomSources.Source.Next(from, count);
if (count > 0) return (int)(from + RandomProviders.Provider.Next((uint)count));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Random(int count) => RandomSources.Source.Next(count);
return (int)(from - RandomProviders.Provider.Next((uint)-count));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint Random(uint count) => RandomSources.Source.Next(count);
public static int Random(int count) => (int)RandomProviders.Provider.Next((uint)count);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RandomBytes(Span<byte> buffer) => RandomSources.Source.NextBytes(buffer);
public static uint Random(uint count) => RandomProviders.Provider.Next(count);
public static void RandomBytes(byte[] buffer) => RandomProviders.Provider.GetBytes(buffer);
public static void RandomBytes(Span<byte> buffer) => RandomProviders.Provider.GetBytes(buffer);
public static double RandomDouble() => RandomProviders.Provider.NextDouble();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double RandomDouble() => RandomSources.Source.NextDouble();
/// <summary>
/// Random pink, blue, green, orange, red or yellow hue
@ -989,13 +990,7 @@ namespace Server
/// </summary>
public static int RandomMetalHue() => Random(2401, 30);
public static int ClipDyedHue(int hue)
{
if (hue < 2)
return 2;
return hue > 1001 ? 1001 : hue;
}
public static int ClipDyedHue(int hue) => hue < 2 ? 2 : hue > 1001 ? 1001 : hue;
/// <summary>
/// Random hue in the range 2-1001

View file

@ -1,265 +0,0 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: Xoshiro256PlusPlus.cs *
* Created: 2019/12/29 - Updated: 2020/05/24 *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using Server.Utilities;
namespace Server
{
public class Xoshiro256PlusPlus : RandomNumberGenerator, IRandomProvider
{
private ulong _s0, _s1, _s2, _s3;
public Xoshiro256PlusPlus() : this((ulong)Environment.TickCount64)
{
}
public Xoshiro256PlusPlus(ulong seed)
{
var mix = new SplitMix64(seed);
var state = new ulong[4];
mix.FillArray(state);
_s0 = state[0];
_s1 = state[1];
_s2 = state[2];
_s3 = state[3];
}
private Xoshiro256PlusPlus(ulong s0, ulong s1, ulong s2, ulong s3)
{
_s0 = s0;
_s1 = s1;
_s2 = s2;
_s3 = s3;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint Next(uint max)
{
if (max <= 1u << 12)
{
if (max == 0) throw new ArgumentOutOfRangeException(nameof(max));
return (uint)(((ulong)NextUInt32() * max) >> 32);
}
uint r, v, limit = (uint)-(int)max;
do
{
r = NextUInt32();
v = r % max;
} while (r - v > limit);
return v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint NextUInt32() => (uint)NextUInt64();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong NextUInt64()
{
var r1 = (_s1 << 2) + _s1;
var r2 = (r1 << 7) | (r1 >> 57);
var rslt = (r2 << 3) + r2;
var t = _s1 << 17;
_s2 ^= _s0;
_s3 ^= _s1;
_s1 ^= _s2;
_s0 ^= _s3;
_s2 ^= t;
_s3 = (_s3 << 45) | (_s3 >> 19);
return rslt;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong Next(ulong max)
{
if (max <= uint.MaxValue) return Next((uint)max);
if (max <= 1ul << 38) return (((NextUInt32() * max) >> 32) + (NextUInt32() & ((1u << 26) - 1)) * max) >> 26;
ulong r, v, limit = (ulong)-(long)max;
do
{
r = NextUInt64();
v = r % max;
} while (r - v > limit);
return v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool NextBool() => NextUInt64() < 1ul << 63;
public override void GetBytes(byte[] data)
{
if (data == null) throw new ArgumentNullException(nameof(data));
GetBytes(new Span<byte>(data));
}
public override unsafe void GetBytes(Span<byte> b)
{
if (b.Length == 0) return;
var s0 = _s0;
var s1 = _s1;
var s2 = _s2;
var s3 = _s3;
var i = 0;
fixed (byte* pBuffer = b)
{
var pULong = (ulong*)pBuffer;
for (var bound = b.Length / sizeof(ulong); i < bound; i++)
{
var r1 = (s1 << 2) + s1;
var r2 = (r1 << 7) | (r1 >> 57);
pULong[i] = (r2 << 3) + r2;
var t = s1 << 17;
s2 ^= s0;
s3 ^= s1;
s1 ^= s2;
s0 ^= s3;
s2 ^= t;
s3 = (s3 << 45) | (s3 >> 19);
}
}
i *= 8;
if (i < b.Length)
{
var r1 = (s1 << 2) + s1;
var r2 = (r1 << 7) | (r1 >> 57);
var rslt = (r2 << 3) + r2;
var t = s1 << 17;
s2 ^= s0;
s3 ^= s1;
s1 ^= s2;
s0 ^= s3;
s2 ^= t;
s3 = (s3 << 45) | (s3 >> 19);
while (i < b.Length)
{
b[i++] = (byte)rslt;
rslt >>= 8;
}
}
_s0 = s0;
_s1 = s1;
_s2 = s2;
_s3 = s3;
}
public double NextDouble() => (NextUInt64() >> 11) * (1.0 / (1ul << 53));
private static readonly ulong[] JUMP =
{ 0x180ec6d33cfd0aba, 0xd5a61266f0c9392c, 0xa9582618e03fc9aa, 0x39abdc4529b1661c };
private static readonly ulong[] LONG_JUMP =
{ 0x76e15d3efefdcbbf, 0xc5004e441c522fb3, 0x77710069854ee241, 0x39109bb02acbe635 };
public void Jump() => Jump(JUMP);
public void LongJump() => Jump(LONG_JUMP);
private void Jump(in ulong[] jumps)
{
ulong s0 = 0;
ulong s1 = 0;
ulong s2 = 0;
ulong s3 = 0;
for (var i = 0; i < jumps.Length; i++)
for (var b = 0; b < 64; b++)
{
if ((jumps[i] & (1ul << b)) != 0)
{
s0 ^= _s0;
s1 ^= _s1;
s2 ^= _s2;
s3 ^= _s3;
}
NextUInt64();
}
_s0 = s0;
_s1 = s1;
_s2 = s2;
_s3 = s3;
}
public Xoshiro256PlusPlus Split()
{
var rng = new Xoshiro256PlusPlus(_s0, _s1, _s2, _s3);
rng.Jump();
return rng;
}
public Xoshiro256PlusPlus LongSplit()
{
var rng = new Xoshiro256PlusPlus(_s0, _s1, _s2, _s3);
rng.LongJump();
return rng;
}
}
public class SplitMix64
{
private ulong x;
public SplitMix64(ulong seed) => x = seed;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong Next()
{
var z = x += 0x9e3779b97f4a7c15;
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
return z ^ (z >> 31);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FillArray(ulong[] arr)
{
for (var i = 0; i < arr.Length; i++) arr[i] = Next();
}
}
}