Randomizer.Double returns Infinity for certain inputs
- Dominant language
- C#
- Stars
- 9.7k
- Forks
- 538
- PR merge metrics
- No merged PRs in 30d
Description
Hi @bchavez!
### Version Information
| Software | Version(s) |
| ------------------------| ---------- |
| Bogus NuGet Package | 34.0.1 |
| .NET Core? | .NET 6 SDK 6.0.200 |
| Windows OS? | Version 20H2 Enterprise |
| Visual Studio? | Microsoft Visual Studio Enterprise 2022 (64-bit) - Current, version 17.1.0 |
### What locale are you using with Bogus?
`en`
### What is the expected behavior?
Calling `Random.Double(double.MinValue, double.MaxValue)` should generated a `double` between these constraints.
### What is the actual behavior?
The method always returns `Infinity`.
This does not happen though when using e.g.
- `double.MinValue, 0`
- or `0, double.MaxValue`.
A quick look at the `Double` method revealed that there is no magnitute/value limitation in line 186.
In particular, the calculation `(max - min) + min` causes this behavior.
Similar behavior can be reproduced for the `Float` and `Decimal` randomizers.
### Please provide a stack trace.
> Bogus.dll!Bogus.Randomizer.Double(double min, double max) Line 186 C#
### Any possible solutions?
See proposed solution in PR #429.
### How do you reproduce the issue?
See above.
### Do you have a unit test that can demonstrate the bug?
Added to PR #429.
### Can you identify the location in Bogus' source code where the problem exists?
See above.
### If the bug is confirmed, would you be willing to submit a PR?
Submitted PR #429.
Contributor guide
Research direction
Review the Randomizer.Double method at line 186 and the proposed solution in PR #429, along with the unit test added there. Check the analogous Float and Decimal randomizers for the same overflow pattern. Done means the full-range inputs no longer return Infinity and the regression tests cover the reported cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100