Repository URL to install this package:
|
Version:
2023.12.1 ▾
|
using System;
namespace Gs2.Unity.Gs2StateMachine.Local
{
public class SharedRandom
{
private const uint M = 10;
private static uint[] RoundFunction(uint[] x, uint[] k)
{
const uint rand0 = 0xD2511F53;
const uint rand1 = 0xCD9E8D57;
return new[]
{
rand0 * x[0] ^ x[1] ^ k[0],
rand1 * x[1] ^ k[1],
};
}
private static uint[] KeySchedule(uint[] k)
{
const uint randWeylConstant = 0x9E3779B9;
const uint randWeylIncrement = 0xBB67AE85;
k[0] += randWeylConstant;
k[1] += randWeylIncrement;
return k;
}
private readonly uint[] _seed;
private readonly uint[] _counter;
public SharedRandom(uint seed, uint category)
{
this._seed = new uint[] { seed, category };
this._counter = new uint[] { 0, 0 };
}
public void SetCounter(uint counter)
{
this._counter[0] = counter;
}
public uint Next()
{
var c = (uint[])this._counter.Clone();
var s = (uint[])this._seed.Clone();
for (uint i = 0; i < M; i++)
{
c = RoundFunction(c, s);
s = KeySchedule(s);
}
this._counter[0]++;
if (this._counter[0] == 0)
{
this._counter[1]++;
}
return c[0];
}
}
}