Repository navigation
Keep NextDouble(min, max) below max and finite for wide bounds [patch] #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright (c) 2023-2026 ktsu-dev contributors | ||
|
|
||
| namespace ktsu.Essentials.Tests; | ||
|
|
||
| using ktsu.Essentials; | ||
| using ktsu.Essentials.DistributionProviders.Uniform; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System; | ||
|
|
||
| /// <summary> | ||
| /// Pins <see cref="IRandomProvider.NextDouble(double, double)"/> and the uniform distribution at the edges | ||
| /// a random sample is unlikely to reach: the top unit draw, and finite bounds whose width overflows. | ||
| /// </summary> | ||
| [TestClass] | ||
| public class RandomRangeBoundaryTests | ||
| { | ||
| [TestMethod] | ||
| [DataRow(1.0, 1.0000000000000002)] | ||
| [DataRow(1.0, 2.0)] | ||
| [DataRow(0.3, 0.7)] | ||
| [DataRow(5.0, 5.5)] | ||
| [DataRow(0.9, 1.0)] | ||
| public void NextDouble_Never_Returns_The_Exclusive_Bound_At_The_Top_Draw(double minInclusive, double maxExclusive) | ||
| { | ||
| IRandomProvider random = new ConstantRandomProvider(ulong.MaxValue); | ||
|
|
||
| double value = random.NextDouble(minInclusive, maxExclusive); | ||
|
|
||
| Assert.IsTrue(value >= minInclusive && value < maxExclusive, $"NextDouble({minInclusive:R}, {maxExclusive:R}) returned {value:R}"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DataRow(ulong.MaxValue)] | ||
| [DataRow(0UL)] | ||
| [DataRow(0x8000000000000000UL)] | ||
| public void NextDouble_Stays_Finite_When_The_Width_Overflows(ulong word) | ||
| { | ||
| IRandomProvider random = new ConstantRandomProvider(word); | ||
|
|
||
| double value = random.NextDouble(double.MinValue, double.MaxValue); | ||
|
|
||
| Assert.IsTrue(double.IsFinite(value), $"NextDouble(MinValue, MaxValue) returned {value} for 0x{word:X16}"); | ||
| Assert.IsTrue(value < double.MaxValue, $"NextDouble(MinValue, MaxValue) returned the exclusive bound for 0x{word:X16}"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Uniform_Handles_Bounds_Whose_Width_Overflows() | ||
| { | ||
| IContinuousDistribution uniform = new UniformDistributionProvider(-1e308, 1e308); | ||
|
|
||
| Assert.AreEqual(0.0, uniform.Mean); | ||
| Assert.AreEqual(0.0, uniform.Median); | ||
| Assert.AreEqual(0.5, uniform.Cdf(0.0)); | ||
| Assert.AreEqual(0.75, uniform.Cdf(1e308 / 2.0), 1e-12); | ||
| Assert.AreEqual(0.0, uniform.Quantile(0.5)); | ||
| Assert.AreEqual(-1e308, uniform.Quantile(0.0)); | ||
| Assert.AreEqual(1e308, uniform.Quantile(1.0)); | ||
| Assert.IsTrue(uniform.Pdf(0.0) > 0.0, "Pdf underflowed to 0 inside the support"); | ||
| Assert.IsTrue(double.IsFinite(uniform.Sample(new ConstantRandomProvider(ulong.MaxValue)))); | ||
| Assert.IsTrue(double.IsFinite(uniform.Sample(new ConstantRandomProvider(0UL)))); | ||
| } | ||
|
|
||
| /// <summary>A provider that fills every buffer with the same 64-bit word, for pinning boundary draws.</summary> | ||
| private sealed class ConstantRandomProvider(ulong word) : IRandomProvider | ||
| { | ||
| public void NextBytes(Span<byte> destination) | ||
| { | ||
| byte[] bytes = BitConverter.GetBytes(word); | ||
| for (int i = 0; i < destination.Length; i++) | ||
| { | ||
| destination[i] = bytes[i % bytes.Length]; | ||
| } | ||
| } | ||
| } | ||
| } |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.