yuanshen/Yuanshen.Common/Security/Util/MT19937.cs
wisedev fc2ac1050f First working version of the server
KCP gameserver with packet dispatch, scene streaming, combat, gacha and the
shop, plus a small HTTP server for the login flow and the wish history page.

Game data under assets is pulled out of the client instead of being typed by
hand. The excel tables come from the packed bin container and the spawn points
from the compiled scene scripts.
2026-09-04 02:10:50 +03:00

56 lines
1.6 KiB
C#

namespace Yuanshen.Common.Security.Util;
internal sealed class MT19937
{
private const ulong N = 312;
private const ulong M = 156;
private const ulong MATRIX_A = 0xB5026F5AA96619E9L;
private const ulong UPPER_MASK = 0xFFFFFFFF80000000;
private const ulong LOWER_MASK = 0X7FFFFFFFUL;
private readonly ulong[] _mt = new ulong[N + 1];
private ulong _mti = N + 1;
public MT19937(ulong seed) => Seed(seed);
public void Seed(ulong seed)
{
_mt[0] = seed;
for (_mti = 1; _mti < N; _mti++)
{
_mt[_mti] = 6364136223846793005L * (_mt[_mti - 1] ^ (_mt[_mti - 1] >> 62)) + _mti;
}
}
public ulong Int63()
{
ulong x = 0;
ulong[] mag01 = { 0x0UL, MATRIX_A };
if (_mti >= N)
{
ulong kk;
if (_mti == N + 1) Seed(5489UL);
for (kk = 0; kk < N - M; kk++)
{
x = (_mt[kk] & UPPER_MASK) | (_mt[kk + 1] & LOWER_MASK);
_mt[kk] = _mt[kk + M] ^ (x >> 1) ^ mag01[x & 0x1UL];
}
for (; kk < N - 1; kk++)
{
x = (_mt[kk] & UPPER_MASK) | (_mt[kk + 1] & LOWER_MASK);
_mt[kk] = _mt[kk - M] ^ (x >> 1) ^ mag01[x & 0x1UL];
}
x = (_mt[N - 1] & UPPER_MASK) | (_mt[0] & LOWER_MASK);
_mt[N - 1] = _mt[M - 1] ^ (x >> 1) ^ mag01[x & 0x1UL];
_mti = 0;
}
x = _mt[_mti++];
x ^= (x >> 29) & 0x5555555555555555L;
x ^= (x << 17) & 0x71D67FFFEDA60000L;
x ^= (x << 37) & 0xFFF7EEE000000000L;
x ^= x >> 43;
return x;
}
}