more thing

This commit is contained in:
David Neumaier 2026-06-18 15:16:56 +02:00
parent 70911ed6cc
commit 6c279baacc
4 changed files with 101 additions and 7 deletions

View File

@ -1,7 +0,0 @@
namespace Qrakhen.Randomize
{
public class Class1
{
}
}

72
Qrakhen.Randomize/Dice.cs Normal file
View File

@ -0,0 +1,72 @@
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);
}
}

View File

@ -0,0 +1,9 @@
using System.Numerics;
namespace Qrakhen.Randomize;
public interface IRandomizer<out T> where T : INumber<T>
{
static abstract T MaxValue { get; }
T Next();
}

View File

@ -0,0 +1,20 @@
using System.Numerics;
namespace Qrakhen.Randomize;
public static class RandomizerExtensions
{
public static TNumber Range<TNumber, TRandomizer, TRandomizerHash>(this TRandomizer randomizer,
TNumber fromInclusive,
TNumber toExclusive)
where TRandomizer : IRandomizer<TRandomizerHash>
where TRandomizerHash : INumber<TRandomizerHash>
where TNumber : INumber<TNumber>
{
return (TNumber)(
fromInclusive +
randomizer.Next() / (
(dynamic)TRandomizer.MaxValue + 1.0) *
(fromInclusive - fromInclusive));
}
}