public void NextFloatTest() { void Test(Random random, float min, float max) { for (FloatScale scale = 0; scale <= FloatScale.ForceLogarithmic; scale++) { Console.Write($@"Random float {min.ToRoundtripString()}..{max.ToRoundtripString()} {scale}: "); float result; try { result = random.NextSingle(min, max, scale); Console.WriteLine(result.ToRoundtripString()); } catch (Exception e) { Console.WriteLine($@"{e.GetType().Name}: {e.Message}".Replace(Environment.NewLine, " ")); throw; } Assert.IsTrue(result >= min && result <= max); } } var rnd = new Random(); Test(rnd, Single.MinValue, Single.MaxValue); Test(rnd, 0, Single.Epsilon); Test(rnd, Single.MaxValue, Single.PositiveInfinity); Test(rnd, Single.NegativeInfinity, Single.PositiveInfinity); }
public void NextDoubleTest() { void Test(Random random, double min, double max) { for (FloatScale scale = 0; scale <= FloatScale.ForceLogarithmic; scale++) { Console.Write($@"Random double {min.ToRoundtripString()}..{max.ToRoundtripString()} ({scale}): "); double result; try { result = random.NextDouble(min, max, scale); Console.WriteLine(result.ToRoundtripString()); } catch (Exception e) { Console.WriteLine($@"{e.GetType().Name}: {e.Message}".Replace(Environment.NewLine, " ")); throw; } Assert.IsTrue(result >= min && result <= max); } } var rnd = new Random(); // edge cases Test(rnd, Double.MinValue, Double.MaxValue); Test(rnd, Double.NegativeInfinity, Double.PositiveInfinity); Test(rnd, 0, Double.PositiveInfinity); Test(rnd, Double.MaxValue, Double.PositiveInfinity); Test(rnd, Double.NegativeInfinity, Double.MinValue); Test(rnd, 1.7976931348623155E+308, Double.MaxValue); Throws <ArgumentOutOfRangeException>(() => Test(rnd, Double.PositiveInfinity, Double.PositiveInfinity)); Throws <ArgumentOutOfRangeException>(() => Test(rnd, Double.NegativeInfinity, Double.NegativeInfinity)); Throws <ArgumentOutOfRangeException>(() => Test(rnd, 0, Double.NaN)); Test(rnd, 0, Double.Epsilon); Test(rnd, Double.Epsilon, Double.Epsilon * 4); Test(rnd, Double.MaxValue / 4, Double.MaxValue); Test(rnd, Double.MaxValue / 2, Double.MaxValue); Test(rnd, -Double.Epsilon, Double.Epsilon); Test(rnd, 0.000000001, 0.0000000011); Test(rnd, 10000, 11000); // big range Test(rnd, Int64.MinValue, Int64.MaxValue); Test(rnd, Int64.MinValue, 0); Test(rnd, Int64.MaxValue, float.MaxValue); Test(rnd, -0.1, UInt64.MaxValue); // very imbalanced positive-negative ranges Test(rnd, 1L << 52, (1L << 54) + 10); // narrow exponent range Test(rnd, Int64.MaxValue, (double)Int64.MaxValue * 4 + 10000); // small exponent range Test(rnd, (double)Int64.MaxValue * 1024, (double)Int64.MaxValue * 4100); // small exponent range Test(rnd, (double)Int64.MinValue * 4100, (double)Int64.MinValue * 1024); // small exponent range // small range Test(rnd, Int64.MaxValue, (double)Int64.MaxValue * 4); Test(rnd, Int64.MaxValue, (double)Int64.MaxValue * 4 + 1000); Test(rnd, 1L << 53, (1L << 53) + 2); Test(rnd, 1L << 52, 1L << 53); }
public void NextDecimalTest() { var rnd = new Random(); void Test(decimal min, decimal max) { for (FloatScale scale = 0; scale <= FloatScale.ForceLogarithmic; scale++) { Console.Write($@"Random decimal {min.ToRoundtripString()}..{max.ToRoundtripString()} ({scale}): "); decimal result; try { result = rnd.NextDecimal(min, max, scale); Console.WriteLine(result.ToRoundtripString()); } catch (Exception e) { Console.WriteLine($@"{e.GetType().Name}: {e.Message}".Replace(Environment.NewLine, " ")); throw; } Assert.IsTrue(result >= min && result <= max); } } // edge cases Test(Decimal.MinValue, Decimal.MaxValue); Test(0, Decimal.MaxValue); Test(0, DecimalExtensions.Epsilon); Test(DecimalExtensions.Epsilon, DecimalExtensions.Epsilon * 4); Test(Decimal.MaxValue / 2, Decimal.MaxValue); Test(Decimal.MaxValue - 1, Decimal.MaxValue); Test(Decimal.MaxValue - DecimalExtensions.Epsilon, Decimal.MaxValue); Test(-DecimalExtensions.Epsilon, DecimalExtensions.Epsilon); Test(0.000000001m, 0.0000000011m); Test(10000, 11000); // big range Test(Int64.MinValue, Int64.MaxValue); Test(Int64.MinValue, 0); Test(-0.1m, UInt64.MaxValue); // very imbalanced positive-negative ranges Test(1L << 52, (1L << 54) + 10); // narrow exponent range Test(Int64.MaxValue, (decimal)Int64.MaxValue * 4 + 10000); // small exponent range Test((decimal)Int64.MaxValue * 1024, (decimal)Int64.MaxValue * 4100); // small exponent range Test((decimal)Int64.MinValue * 4100, (decimal)Int64.MinValue * 1024); // small exponent range // small range Test(Int64.MaxValue, (decimal)Int64.MaxValue * 4); Test(Int64.MaxValue, (decimal)Int64.MaxValue * 4 + 1000); Test(1L << 53, (1L << 53) + 2); Test(1L << 52, 1L << 53); }