Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions PCGSharp/Source/Pcg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 6 additions & 5 deletions PCGSharp/Source/PcgExtended.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down