Repository navigation
Keep NextDouble(min, max) below max and finite for wide bounds [patch] - #93
Merged
Merged
Conversation
Rounding the final addition could land exactly on the exclusive upper bound when the unit draw was close to 1, and finite bounds of opposite sign overflowed the width to Infinity, so NextDouble(MinValue, MaxValue) returned Infinity or NaN. Interpolate between the bounds when the width overflows and step down from max when the result reaches it. UniformDistributionProvider had the same overflow in Mean, Median, Cdf, Quantile, Pdf and Sample; they now use half-width forms that stay finite. Fixes #61 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ABXREnMPNwAYQMdr4STcfE
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ABXREnMPNwAYQMdr4STcfE
|
This was referenced Oct 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #61
What was wrong
IRandomProvider.NextDouble(min, max)computedmin + u * (max - min). When the unit draw was close to 1, rounding could land exactly onmaxExclusive. For example,NextDouble(1.0, BitIncrement(1.0))returned max about half the time.NextDouble(double.MinValue, double.MaxValue)returnedInfinity, orNaNwhen the unit draw was 0.UniformDistributionProviderhad the same width overflow, which brokeMean,Median,Cdf,Quantile,PdfandSample.Change
NextDouble(min, max), when the width is infinite it interpolates asmin*(1-u) + max*uinstead. If the result reachesmax(andmax > min), it returns the next double belowmax.RandomHelpers.NextDown, becauseMath.BitDecrementis not available on netstandard2.1.UniformDistributionProvider:Meanis nowMin/2 + Max/2.Pdfnow uses a half width, which stays finite.CdfandQuantileswitch to half-width and interpolation forms when the full width overflows.Medianfollows fromQuantile, andSamplefollows fromNextDouble.Tests
New
RandomRangeBoundaryTestsuses a constant-word stub provider:NextDoublestays below max for(1, 1+ulp),(1, 2),(0.3, 0.7),(5, 5.5)and(0.9, 1).NextDouble(MinValue, MaxValue)stays finite and below max for the draws0,2^63andulong.MaxValue.Uniform(-1e308, 1e308)givesMean == 0,Median == 0,Cdf(0) == 0.5,Quantile(0.5) == 0, a positivePdf, and a finiteSample.With the fix reverted all 9 cases fail; with it they pass. The full solution builds for every target framework, including netstandard2.1. The full suite passes: 935 tests, 0 failed.
This PR stands alone on main. It also test-merges cleanly with #92 (the related #60 fix), which touches a neighbouring line in
RandomHelpers.cs.🤖 Generated with Claude Code
https://claude.ai/code/session_01ABXREnMPNwAYQMdr4STcfE
Generated by Claude Code