diff --git a/PCGSharp/Source/Pcg.cs b/PCGSharp/Source/Pcg.cs index e854194..6f311b5 100644 --- a/PCGSharp/Source/Pcg.cs +++ b/PCGSharp/Source/Pcg.cs @@ -68,7 +68,9 @@ public class Pcg { const ulong ShiftedIncrement = 721347520444481703ul; ulong _increment = 1442695040888963407ul; const ulong Multiplier = 6364136223846793005ul; + const float ToFloat01 = 1.0f / 4294967296.0f; const double ToDouble01 = 1.0 / 4294967296.0; + const uint HalfUint = uint.MaxValue / 2; // This attribute ensures that every thread will get its own instance of PCG. // An alternative, since PCG supports streams, is to use a different stream per @@ -215,21 +217,21 @@ public uint[] NextUInts(int count, uint minInclusive, uint maxExclusive) { } public float NextFloat() { - return (float)(NextUInt() * ToDouble01); + return NextUInt() * ToFloat01; } public float NextFloat(float maxInclusive) { if(maxInclusive <= 0) throw new ArgumentException("MaxInclusive must be larger than 0"); - return (float)(NextUInt() * ToDouble01) * maxInclusive; + return NextUInt() * ToFloat01 * maxInclusive; } public float NextFloat(float minInclusive, float maxInclusive) { if(maxInclusive < minInclusive) throw new ArgumentException("Max must be larger than min"); - return (float)(NextUInt() * ToDouble01) * (maxInclusive-minInclusive) + minInclusive; + return NextUInt() * ToFloat01 * (maxInclusive-minInclusive) + minInclusive; } public float[] NextFloats(int count) { @@ -333,8 +335,7 @@ public byte[] NextBytes(int count) { } public bool NextBool() { - var result = NextUInt(); - return result % 2 == 1; + return NextUInt() < HalfUint; } public bool[] NextBools(int count) { diff --git a/PCGSharp/Source/PcgExtended.cs b/PCGSharp/Source/PcgExtended.cs index f3b7693..f0422d4 100644 --- a/PCGSharp/Source/PcgExtended.cs +++ b/PCGSharp/Source/PcgExtended.cs @@ -94,7 +94,9 @@ public class PcgExtended { const uint McgUnmultiplier = 2897767785u; // 1 / (uint.MaxValue + 1) + const float ToFloat01 = 1.0f / 4294967296.0f; const double ToDouble01 = 1.0 / 4294967296.0; + const uint HalfUint = uint.MaxValue / 2; uint[] _data; // This attribute ensures that every thread will get its own instance of PCG. @@ -265,21 +267,21 @@ public uint[] NextUInts(int count, uint minInclusive, uint maxExclusive) { } public float NextFloat() { - return (float)(NextUInt() * ToDouble01); + return (NextUInt() * ToFloat01); } public float NextFloat(float maxInclusive) { if(maxInclusive <= 0) throw new ArgumentException("Max must be larger than 0"); - return (float)(NextUInt() * ToDouble01) * maxInclusive; + return (NextUInt() * ToFloat01) * maxInclusive; } public float NextFloat(float minInclusive, float maxInclusive) { if(maxInclusive < minInclusive) throw new ArgumentException("Max must be larger than min"); - return (float)(NextUInt() * ToDouble01) * (maxInclusive-minInclusive) + minInclusive; + return (NextUInt() * ToFloat01) * (maxInclusive-minInclusive) + minInclusive; } public float[] NextFloats(int count) { @@ -383,8 +385,7 @@ public byte[] NextBytes(int count) { } public bool NextBool() { - uint result = NextUInt(); - return ((result % 2) == 1); + return NextUInt() < HalfUint; } public bool[] NextBools(int count) {