Files
Workspace/Assets/Demos/Demo2/Domain/DeterministicRandom.cs
T
2026-08-18 02:03:34 +08:00

35 lines
1004 B
C#

#nullable enable
namespace Demo2.Domain
{
public struct DeterministicRandom
{
private ulong _state;
private ulong _increment;
public DeterministicRandom(ulong seed, ulong stream = 54)
{
_state = 0;
_increment = (stream << 1) | 1;
NextUInt();
_state += seed;
NextUInt();
}
public uint NextUInt()
{
var oldState = _state;
_state = unchecked(oldState * 6364136223846793005UL + _increment);
var xorShifted = (uint)(((oldState >> 18) ^ oldState) >> 27);
var rotation = (int)(oldState >> 59);
return (xorShifted >> rotation) | (xorShifted << ((-rotation) & 31));
}
public int NextInt(int minInclusive, int maxExclusive)
{
if (maxExclusive <= minInclusive) return minInclusive;
return minInclusive + (int)(NextUInt() % (uint)(maxExclusive - minInclusive));
}
}
}