public void UniformRandomDistribution_WithRandomNumberMethod_Test() { const int numIterations = 100_000; const int range = 500; var distribution = new int[range]; for (int i = 0; i < numIterations; i++) { var value = _sut.RandomNumber(range); distribution[value]++; } var expectedValue = numIterations / range; var stdDev = range - 1 / Math.Sqrt(12); var outliers = distribution .Where(i => i <expectedValue - stdDev || i> expectedValue + stdDev) .ToArray(); if (outliers.Any()) { _output.WriteLine($"Expected value: {expectedValue}"); foreach (var outlier in outliers) { var difference = Math.Max(outlier, expectedValue) - Math.Min(outlier, expectedValue); _output.WriteLine($"{outlier}: diff of {difference} ({difference / stdDev}% of std. dev)"); } Assert.True(false, "Outliers detected"); } }
public void UniformRandomDistribution_WithRandomNumberMethod_Test() { const int numIterations = 100000; const int range = 100; var distribution = new int[range]; for (int i = 0; i < numIterations; i++) { var value = _sut.RandomNumber(range); distribution[value]++; } var expectedValue = numIterations / range; var stdDev = 0.1 * expectedValue; Assert.True(distribution.All(i => i >= expectedValue - stdDev && i <= expectedValue + stdDev)); }