public void AverageDoubles(Double low, Double high, UInt64 seed) { const Int32 iterations = 10_000; var populationMean = high / 2 + low / 2; var popStdDev = 1.0 / Math.Sqrt(3) * (high / 2 - low / 2); var exclusiveDist = Uniform.New(low, high); var inclusiveDist = Uniform.NewInclusive(low, high); var rng = Pcg32.Create(789 + seed, 11634580027462260723ul); Double exclusiveMean = 0; Double inclusiveMean = 0; for (var i = 0; i < iterations; i++) { var exclusive = exclusiveDist.Sample(rng); var exclusiveDelta = exclusive - exclusiveMean; exclusiveMean += exclusiveDelta / (i + 1); Assert.True(low <= exclusive); Assert.True(exclusive < high); var inclusive = inclusiveDist.Sample(rng); var inclusiveDelta = inclusive - inclusiveMean; inclusiveMean += inclusiveDelta / (i + 1); Assert.True(low <= inclusive); Assert.True(inclusive < high); } Assert.True(Statistics.WithinConfidence(populationMean, popStdDev, exclusiveMean, iterations)); Assert.True(Statistics.WithinConfidence(populationMean, popStdDev, inclusiveMean, iterations)); }
public void BadExclusiveRange(Int64 highInt, Int64 lowInt) { var low = TimeSpan.FromTicks(lowInt); var high = TimeSpan.FromTicks(highInt); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(high, low)); }
public void AverageDecimals(Decimal low, Decimal high, UInt64 seed) { const Int32 iterations = 10_000; const Decimal sqr3 = 1.7320508075688772935274463415m; var populationMean = high / 2 + low / 2; var popStdDev = 1.0m / sqr3 * (high / 2 - low / 2); var exclusiveDist = Uniform.New(low, high); var inclusiveDist = Uniform.NewInclusive(low, high); var rng = Pcg32.Create(789 + seed, 11634580027462260723ul); Decimal exclusiveMean = 0; Decimal inclusiveMean = 0; for (var i = 0; i < iterations; i++) { var exclusive = exclusiveDist.Sample(rng); var exclusiveDelta = exclusive - exclusiveMean; exclusiveMean += exclusiveDelta / (i + 1); Assert.True(low <= exclusive); Assert.True(exclusive < high); var inclusive = inclusiveDist.Sample(rng); var inclusiveDelta = inclusive - inclusiveMean; inclusiveMean += inclusiveDelta / (i + 1); Assert.True(low <= inclusive); Assert.True(inclusive < high); } Assert.True(Statistics.WithinConfidence(populationMean, popStdDev, exclusiveMean, iterations)); Assert.True(Statistics.WithinConfidence(populationMean, popStdDev, inclusiveMean, iterations)); }
public void TrySampleDouble() { var rng = Pcg32.Create(789, 11634580027462260723ul); var dist = Uniform.New((Double)0, (Double)100); for (Int32 i = 0; i < 10_000; i++) { Assert.True(dist.TrySample(rng, out _)); } }
public void SampleExclusiveInt64(Int64 low, Int64 high) { var dist = Uniform.New(low, high); var rng = Pcg32.Create(252, 11634580027462260723ul); for (var i = 0; i < 10000; i++) { var result = dist.Sample(rng); Assert.True(low <= result); Assert.True(result < high); } }
public void SampleExclusive(Int64 lowInt, Int64 highInt) { var low = TimeSpan.FromTicks(lowInt); var high = TimeSpan.FromTicks(highInt); var dist = Uniform.New(low, high); var rng = Pcg32.Create(252, 11634580027462260723ul); for (var i = 0; i < 10000; i++) { var result = dist.Sample(rng); Assert.True(low <= result); Assert.True(result < high); } }
public void BadRange() { // Equal Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(0m, 0m)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(Decimal.MaxValue, Decimal.MaxValue)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(Decimal.MinValue, Decimal.MinValue)); // Reversed Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(10.0m, 10.0m)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(9001.0m, -666.0m)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(Decimal.MaxValue, Decimal.MinValue)); // Reversed Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.NewInclusive(10.0m, 9.0m)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.NewInclusive(9001.0m, -666.0m)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.NewInclusive(Decimal.MaxValue, Decimal.MinValue)); }
public void DoubleRange(Double low, Double high) { var rng = Pcg32.Create(252, 11634580027462260723ul); var zeroRng = new StepRng(0) { Increment = 0 }; var maxRng = new StepRng(0xFFFF_FFFF_FFFF_FFFF) { Increment = 0 }; var uniform = Uniform.New(low, high); var inclusiveUniform = Uniform.NewInclusive(low, high); for (var i = 0; i < 100; i++) { var exclusive = uniform.Sample(rng); Assert.True(low <= exclusive && exclusive < high, $"Exclusive sampling of RNG failed; low: {low}, high: {high}, actual: {exclusive}"); var inclusive = uniform.Sample(rng); Assert.True(low <= inclusive && inclusive <= high, $"Inclusive sampling of RNG failed; low: {low}, high: {high}, actual: {inclusive}"); } Assert.Equal(low, Uniform.NewInclusive(low, low).Sample(rng)); Assert.Equal(low, uniform.Sample(zeroRng)); Assert.Equal(low, inclusiveUniform.Sample(zeroRng)); Assert.True(uniform.Sample(maxRng) < high, $"Exclusive sampling of Max RNG failed; low: {low}, high: {high}, actual: {uniform.Sample(maxRng)}"); Assert.True(inclusiveUniform.Sample(maxRng) <= high, $"Inclusive sampling of Max RNG failed; low: {low}, high: {high}, actual: {inclusiveUniform.Sample(maxRng):G19}"); // Don't run this test for really tiny differences between high and low // since for those rounding might result in selecting high for a very // long time. if (high - low <= 0.0001) { return; } var loweringMaxRng = new StepRng(0xFFFF_FFFF_FFFF_FFFF) { Increment = unchecked ((UInt64)(-1L << 12)) }; Assert.True(uniform.Sample(loweringMaxRng) < high); Assert.True(uniform.Sample(loweringMaxRng) < high); }
public void FloatTest(Decimal low, Decimal high) { var rng = Pcg32.Create(252, 11634580027462260723ul); var zeroRng = new StepRng(0) { Increment = 0 }; var maxRng = new StepRng(0xFFFF_FFFF_FFFF_FFFF) { Increment = 0 }; var exclusiveUniform = Uniform.New(low, high); var inclusiveUniform = Uniform.NewInclusive(low, high); for (var i = 0; i < 100; i++) { var exclusive = exclusiveUniform.Sample(rng); Assert.True(low <= exclusive && exclusive < high); var inclusive = exclusiveUniform.Sample(rng); Assert.True(low <= inclusive && inclusive <= high); } Assert.Equal(low, Uniform.NewInclusive(low, low).Sample(rng)); Assert.Equal(low, exclusiveUniform.Sample(zeroRng)); Assert.Equal(low, inclusiveUniform.Sample(zeroRng)); Assert.True(exclusiveUniform.Sample(maxRng) < high); Assert.True(inclusiveUniform.Sample(maxRng) <= high); var loweringMaxRng = new StepRng(0xFFFF_FFFF_FFFF_FFFF) { Increment = unchecked ((UInt64)(-1L << 12)) }; Assert.True(exclusiveUniform.Sample(loweringMaxRng) < high); Assert.True(exclusiveUniform.Sample(loweringMaxRng) < high); var maxDoubleInclusive = Uniform.NewInclusive(Decimal.MaxValue, Decimal.MaxValue); Assert.Equal(Decimal.MaxValue, maxDoubleInclusive.Sample(rng)); var minDoubleInclusive = Uniform.NewInclusive(-Decimal.MaxValue, -Decimal.MaxValue); Assert.Equal(-Decimal.MaxValue, minDoubleInclusive.Sample(rng)); }
public void NonNullableDouble() { var dist = Uniform.New(1.0d, 2.0d); Assert.Throws <ArgumentNullException>(() => dist.Sample <StepRng>(null)); }
public void BadRange() { // Equal Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(0f, 0f)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(Single.MaxValue, Single.MaxValue)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(Single.MinValue, Single.MinValue)); // Reversed Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(10.0f, 10.0f)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(9001.0f, -666.0f)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(Single.MaxValue, Single.MinValue)); // Infinities Assert.Throws <ArgumentOutOfRangeException>("low", () => Uniform.New(Single.NegativeInfinity, 0)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(0, Single.PositiveInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Single.PositiveInfinity, 0)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(0, Single.NegativeInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Single.PositiveInfinity, Single.PositiveInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Single.NegativeInfinity, Single.NegativeInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Single.NegativeInfinity, Single.PositiveInfinity)); // NaNs Assert.Throws <ArgumentOutOfRangeException>("low", () => Uniform.New(Single.NaN, 0)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(0, Single.NaN)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Single.NaN, Single.NaN)); // Mixed NaN / Infinity Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Single.NaN, Single.PositiveInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Single.PositiveInfinity, Single.NaN)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Single.NaN, Single.NegativeInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Single.NegativeInfinity, Single.NaN)); // Reversed Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.NewInclusive(10.0f, 9.0f)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.NewInclusive(9001.0f, -666.0f)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.NewInclusive(Single.MaxValue, Single.MinValue)); // Infinities Assert.Throws <ArgumentOutOfRangeException>("low", () => Uniform.NewInclusive(Single.NegativeInfinity, 0)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.NewInclusive(0, Single.PositiveInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Single.PositiveInfinity, 0)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(0, Single.NegativeInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Single.PositiveInfinity, Single.PositiveInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Single.NegativeInfinity, Single.NegativeInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Single.NegativeInfinity, Single.PositiveInfinity)); // NaNs Assert.Throws <ArgumentOutOfRangeException>("low", () => Uniform.NewInclusive(Single.NaN, 0)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.NewInclusive(0, Single.NaN)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Single.NaN, Single.NaN)); // Mixed NaN / Infinity Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Single.NaN, Single.PositiveInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Single.PositiveInfinity, Single.NaN)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Single.NaN, Single.NegativeInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Single.NegativeInfinity, Single.NaN)); // Equal Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(0d, 0d)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(Double.MaxValue, Double.MaxValue)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(Double.MinValue, Double.MinValue)); // Reversed Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(10.0d, 10.0d)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(9001.0d, -666.0d)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(Double.MaxValue, Double.MinValue)); // Infinities Assert.Throws <ArgumentOutOfRangeException>("low", () => Uniform.New(Double.NegativeInfinity, 0)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(0, Double.PositiveInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Double.PositiveInfinity, 0)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(0, Double.NegativeInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Double.PositiveInfinity, Double.PositiveInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Double.NegativeInfinity, Double.NegativeInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Double.NegativeInfinity, Double.PositiveInfinity)); // NaNs Assert.Throws <ArgumentOutOfRangeException>("low", () => Uniform.New(Double.NaN, 0)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.New(0, Double.NaN)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Double.NaN, Double.NaN)); // Mixed NaN / Infinity Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Double.NaN, Double.PositiveInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Double.PositiveInfinity, Double.NaN)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Double.NaN, Double.NegativeInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(Double.NegativeInfinity, Double.NaN)); // Reversed Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.NewInclusive(10.0d, 9.0d)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.NewInclusive(9001.0d, -666.0d)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.NewInclusive(Double.MaxValue, Double.MinValue)); // Infinities Assert.Throws <ArgumentOutOfRangeException>("low", () => Uniform.NewInclusive(Double.NegativeInfinity, 0)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.NewInclusive(0, Double.PositiveInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Double.PositiveInfinity, 0)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(0, Double.NegativeInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Double.PositiveInfinity, Double.PositiveInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Double.NegativeInfinity, Double.NegativeInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Double.NegativeInfinity, Double.PositiveInfinity)); // NaNs Assert.Throws <ArgumentOutOfRangeException>("low", () => Uniform.NewInclusive(Double.NaN, 0)); Assert.Throws <ArgumentOutOfRangeException>("high", () => Uniform.NewInclusive(0, Double.NaN)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Double.NaN, Double.NaN)); // Mixed NaN / Infinity Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Double.NaN, Double.PositiveInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Double.PositiveInfinity, Double.NaN)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Double.NaN, Double.NegativeInfinity)); Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.NewInclusive(Double.NegativeInfinity, Double.NaN)); }
public void BadExclusiveRangeInt64(Int64 high, Int64 low) { Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(high, low)); }
public void BadExclusiveRangeSByte(SByte high, SByte low) { Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(high, low)); }
public void BadExclusiveRangeUInt32(UInt32 high, UInt32 low) { Assert.Throws <ArgumentOutOfRangeException>(() => Uniform.New(high, low)); }