72 lines
1.4 KiB
C#
72 lines
1.4 KiB
C#
namespace Qrakhen.Randomize;
|
|
|
|
public class Dice : IRandomizer<uint>
|
|
{
|
|
public static uint MaxValue => uint.MaxValue;
|
|
|
|
public static Dice? Global { get; private set; }
|
|
|
|
private const uint P = 0x0100_0193; // prime
|
|
private const uint Q = 0x811C_9DC5; // basis
|
|
|
|
private uint _n;
|
|
|
|
public readonly uint Seed;
|
|
|
|
public int Count { get; private set; } = 0;
|
|
|
|
public Dice() : this((int)((DateTime.UtcNow.Ticks & 0xFFFF_FFFF) ^ Q)) { }
|
|
|
|
public Dice(int seed)
|
|
{
|
|
Seed = (uint)seed;
|
|
_n = StrongHash(Q, Seed);
|
|
}
|
|
|
|
//public float ExpRange(float from, float to, float q = .5f)
|
|
//{
|
|
// q = Mathf.Clamp(q, 0, 1);
|
|
// float linear = Range(0f, 1f);
|
|
// float exponent = Mathf.Pow(2f, (0.5f - q) * 4f);
|
|
// float curvedRandom = Mathf.Pow(linear, exponent);
|
|
// return Mathf.Lerp(from, to, curvedRandom);
|
|
//}
|
|
|
|
public uint Next()
|
|
{
|
|
unchecked
|
|
{
|
|
_n ^= Q;
|
|
_n *= P;
|
|
Count++;
|
|
|
|
return (_n);
|
|
}
|
|
}
|
|
|
|
public uint FastHash(uint v)
|
|
{
|
|
return (v ^ Q) * P;
|
|
}
|
|
|
|
public uint StrongHash(uint v, uint q)
|
|
{
|
|
uint r = v;
|
|
r = (r ^ (q & 0xFF)) * P;
|
|
r = (r ^ ((q >> 0x08) & 0xFF)) * P;
|
|
r = (r ^ ((q >> 0x10) & 0xFF)) * P;
|
|
r = (r ^ ((q >> 0x18) & 0xFF)) * P;
|
|
|
|
return r;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"Dice <Seed: {Seed}, Count: {Count}, N: 0x{_n:x8}>";
|
|
}
|
|
|
|
public static void InitGlobal(int seed)
|
|
{
|
|
Global = new Dice(seed);
|
|
}
|
|
} |