From e912115bf4317ab7f599f0175ae0d5343c1e3fd1 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sat, 14 Nov 2026 14:07:43 +0100 Subject: [PATCH] GenerateRandomDoubles: net11.0, uniform secure double, Random.Shared, real tests Fix GetSecureDouble to divide a secure integer by int.MaxValue instead of dividing 1 by it, drop the unused RandomNumberGenerator.Create() call and the redundant casts, use Random.Shared, add the below-upper-bound and wide-range helpers, run Main once with the article's values, and replace the four IsNotNull tests with eight tests that check intervals, spread and the rounding edge cases. MSTest 4.5.3, Microsoft.NET.Test.Sdk 18.12.0, coverlet.collector 10.1.0. --- .../GenerateRandomDoubles.csproj | 2 +- .../GenerateRandomDoubles/Program.cs | 116 +++++++++--------- .../GenerateRandomDoublesTests.csproj | 11 +- .../GenerateRandomNumbersTests.cs | 96 +++++++++++---- 4 files changed, 142 insertions(+), 83 deletions(-) diff --git a/numbers-csharp/GenerateRandomDoubles/GenerateRandomDoubles/GenerateRandomDoubles.csproj b/numbers-csharp/GenerateRandomDoubles/GenerateRandomDoubles/GenerateRandomDoubles.csproj index 74abf5c976..5bf6c9b8cf 100644 --- a/numbers-csharp/GenerateRandomDoubles/GenerateRandomDoubles/GenerateRandomDoubles.csproj +++ b/numbers-csharp/GenerateRandomDoubles/GenerateRandomDoubles/GenerateRandomDoubles.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net11.0 enable enable diff --git a/numbers-csharp/GenerateRandomDoubles/GenerateRandomDoubles/Program.cs b/numbers-csharp/GenerateRandomDoubles/GenerateRandomDoubles/Program.cs index c82fdac0b8..f8cb3c9a43 100644 --- a/numbers-csharp/GenerateRandomDoubles/GenerateRandomDoubles/Program.cs +++ b/numbers-csharp/GenerateRandomDoubles/GenerateRandomDoubles/Program.cs @@ -1,66 +1,72 @@ - -using System.Security.Cryptography; +using System.Security.Cryptography; -namespace GenerateRandomNumbers +namespace GenerateRandomNumbers; + +public class Program { - public class Program + public static void Main(string[] args) + { + var pseudoDouble = GetPseudoDouble(); + Console.WriteLine($"Pseudorandom double: {pseudoDouble}"); + + var pseudoDoubleWithinRange = GetPseudoDoubleWithinRange(16, 30000); + Console.WriteLine($"Pseudorandom double between 16 and 30000: {pseudoDoubleWithinRange}"); + + var secureDouble = GetSecureDouble(); + Console.WriteLine($"Secure double: {secureDouble}"); + + var secureDoubleWithinRange = GetSecureDoubleWithinRange(4, 123000); + Console.WriteLine($"Secure double between 4 and 123000: {secureDoubleWithinRange}"); + + ShowEdgeCases(); + } + + public static double GetPseudoDouble() { - public static void Main(string[] args) - { - var run = true; - while (run) - { - GenerateNumbers(); - Console.WriteLine("\n Run Again? (Y/N)"); - var input = Console.ReadLine() ?? ""; - run = input.ToLower().First().Equals('y') ? true : false; - } - } + var rDouble = Random.Shared.NextDouble(); + return rDouble; + } - private static void GenerateNumbers() - { - var lowerBound = Int32.MinValue; - var upperBound = Int32.MaxValue; + public static double GetPseudoDoubleWithinRange(double lowerBound, double upperBound) + { + var rDouble = Random.Shared.NextDouble(); + var rRangeDouble = rDouble * (upperBound - lowerBound) + lowerBound; + return rRangeDouble; + } - var pseduoDouble = GetPseudoDouble(); - Console.WriteLine($"Pseduo Random Double Generated: {pseduoDouble}"); + public static double GetPseudoDoubleBelowUpperBound(double lowerBound, double upperBound) + { + var rRangeDouble = GetPseudoDoubleWithinRange(lowerBound, upperBound); + return rRangeDouble < upperBound ? rRangeDouble : Math.BitDecrement(upperBound); + } - var pseduoDoubleWithinRange = GetPseudoDoubleWithinRange(lowerBound, upperBound); - Console.WriteLine($"Pseduo Random Double Generated with {lowerBound} as the inclusive lower bound and {upperBound} as the exclusive upper bound: {pseduoDoubleWithinRange}"); + public static double GetPseudoDoubleWithinWideRange(double lowerBound, double upperBound) + { + var rDouble = Random.Shared.NextDouble(); + var rRangeDouble = rDouble * upperBound + (1 - rDouble) * lowerBound; + return rRangeDouble; + } - var secureDouble = GetSecureDouble(); - Console.WriteLine($"Secure Random Double Generated: {secureDouble}"); + public static double GetSecureDouble() + { + var numerator = RandomNumberGenerator.GetInt32(int.MaxValue); + var sDouble = (double)numerator / int.MaxValue; + return sDouble; + } - var secureDoubleWithinRange = GetSecureDoubleWithinRange(lowerBound, upperBound); - Console.WriteLine($"Secure Random Double Generated with {lowerBound} as the inclusive lower bound and {upperBound} as the exclusive upper bound: {secureDoubleWithinRange}"); - } + public static double GetSecureDoubleWithinRange(double lowerBound, double upperBound) + { + var rDouble = GetSecureDouble(); + var rRangeDouble = rDouble * (upperBound - lowerBound) + lowerBound; + return rRangeDouble; + } - public static double GetPseudoDouble() - { - var random = new Random(); - var rDouble = random.NextDouble(); - return rDouble; - } - public static double GetPseudoDoubleWithinRange(double lowerBound, double upperBound) - { - var random = new Random(); - var rDouble = random.NextDouble(); - var rRangeDouble = rDouble * (upperBound - lowerBound) + lowerBound; - return rRangeDouble; - } + public static void ShowEdgeCases() + { + var largest = Math.BitDecrement(1.0); + Console.WriteLine(largest); + Console.WriteLine(largest * (2.0 - 1.0) + 1.0); - public static double GetSecureDouble() - { - RandomNumberGenerator.Create(); - var denominator = RandomNumberGenerator.GetInt32(2, int.MaxValue); - double sDouble = (double) 1 / denominator; - return sDouble; - } - public static double GetSecureDoubleWithinRange(double lowerBound, double upperBound) - { - var rDouble = GetSecureDouble(); - var rRangeDouble = (double)rDouble * (upperBound - lowerBound) + lowerBound; - return rRangeDouble; - } + Console.WriteLine(GetPseudoDoubleWithinRange(double.MinValue, double.MaxValue)); } -} \ No newline at end of file +} diff --git a/numbers-csharp/GenerateRandomDoubles/GenerateRandomDoublesTests/GenerateRandomDoublesTests.csproj b/numbers-csharp/GenerateRandomDoubles/GenerateRandomDoublesTests/GenerateRandomDoublesTests.csproj index b5d92195ac..8d5d35a792 100644 --- a/numbers-csharp/GenerateRandomDoubles/GenerateRandomDoublesTests/GenerateRandomDoublesTests.csproj +++ b/numbers-csharp/GenerateRandomDoubles/GenerateRandomDoublesTests/GenerateRandomDoublesTests.csproj @@ -1,17 +1,18 @@ - net6.0 + net11.0 + enable enable false - - - - + + + + diff --git a/numbers-csharp/GenerateRandomDoubles/GenerateRandomDoublesTests/GenerateRandomNumbersTests.cs b/numbers-csharp/GenerateRandomDoubles/GenerateRandomDoublesTests/GenerateRandomNumbersTests.cs index 69d07ed315..61cfb66710 100644 --- a/numbers-csharp/GenerateRandomDoubles/GenerateRandomDoublesTests/GenerateRandomNumbersTests.cs +++ b/numbers-csharp/GenerateRandomDoubles/GenerateRandomDoublesTests/GenerateRandomNumbersTests.cs @@ -1,42 +1,94 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace GenerateRandomNumbers +namespace GenerateRandomNumbers; + +[TestClass] +public class GenerateRandomNumbersTests { - [TestClass] - public class GenerateRandomNumbersTests + private const int Draws = 1000; + + [TestMethod] + public void WhenCallingGetPseudoDouble_ThenBetweenZeroAndOne() { - [TestMethod] - public void WhenCallingGetPseudoDouble_ThenNotNull() + for (var i = 0; i < Draws; i++) { var num = Program.GetPseudoDouble(); - Assert.IsNotNull(num); + Assert.IsTrue(num >= 0.0 && num < 1.0, $"{num} is outside [0, 1)"); } + } - [TestMethod] - public void WhenCallingGetPseudoDoubleWithinRange_ThenWithinRange() + [TestMethod] + public void WhenCallingGetPseudoDoubleWithinRange_ThenWithinRange() + { + double lowerBound = 4; + double upperBound = 400; + for (var i = 0; i < Draws; i++) { - double lowerBound = 4; - double upperBound = 400; var num = Program.GetPseudoDoubleWithinRange(lowerBound, upperBound); - Assert.IsNotNull(num); - if (num >= upperBound || num < lowerBound) Assert.Fail("Number Not within Range"); + Assert.IsTrue(num >= lowerBound && num < upperBound, $"{num} is outside the range"); } + } - [TestMethod] - public void WhenCallingGetSecureDouble_ThenNotNull() + [TestMethod] + public void WhenCallingGetSecureDouble_ThenBetweenZeroAndOne() + { + for (var i = 0; i < Draws; i++) { var num = Program.GetSecureDouble(); - Assert.IsNotNull(num); + Assert.IsTrue(num >= 0.0 && num < 1.0, $"{num} is outside [0, 1)"); + } + } + + [TestMethod] + public void WhenCallingGetSecureDouble_ThenValuesSpreadAcrossTheInterval() + { + var aboveHalf = 0; + for (var i = 0; i < Draws; i++) + { + if (Program.GetSecureDouble() >= 0.5) aboveHalf++; } - [TestMethod] - public void WhenCallingGetSecureDoubleWithinRange_ThenWithinRange() + Assert.IsTrue(aboveHalf > 0, "No secure double landed in the upper half"); + Assert.IsTrue(aboveHalf < Draws, "Every secure double landed in the upper half"); + } + + [TestMethod] + public void WhenCallingGetSecureDoubleWithinRange_ThenWithinRange() + { + double lowerBound = 5; + double upperBound = 500; + for (var i = 0; i < Draws; i++) { - double lowerBound = 5; - double upperBound = 500; var num = Program.GetSecureDoubleWithinRange(lowerBound, upperBound); - Assert.IsNotNull(num); - if (num >= upperBound || num < lowerBound) Assert.Fail("Number Not within Range"); + Assert.IsTrue(num >= lowerBound && num < upperBound, $"{num} is outside the range"); } } -} \ No newline at end of file + + [TestMethod] + public void WhenScalingTheLargestNextDoubleToOneAndTwo_ThenResultRoundsToUpperBound() + { + var largest = Math.BitDecrement(1.0); + + Assert.AreEqual(2.0, largest * (2.0 - 1.0) + 1.0); + } + + [TestMethod] + public void WhenCallingGetPseudoDoubleBelowUpperBound_ThenBelowUpperBound() + { + for (var i = 0; i < Draws; i++) + { + var num = Program.GetPseudoDoubleBelowUpperBound(1.0, 2.0); + Assert.IsTrue(num >= 1.0 && num < 2.0, $"{num} is outside [1, 2)"); + } + } + + [TestMethod] + public void WhenTheRangeIsWiderThanDoubleMaxValue_ThenOnlyTheWideRangeFormulaStaysFinite() + { + var plain = Program.GetPseudoDoubleWithinRange(double.MinValue, double.MaxValue); + var wide = Program.GetPseudoDoubleWithinWideRange(double.MinValue, double.MaxValue); + + Assert.IsFalse(double.IsFinite(plain)); + Assert.IsTrue(double.IsFinite(wide)); + } +}