Add generic Random NextInteger/NextBinaryFloat APIs#127462
Open
stephentoub wants to merge 1 commit intomainfrom
Open
Add generic Random NextInteger/NextBinaryFloat APIs#127462stephentoub wants to merge 1 commit intomainfrom
stephentoub wants to merge 1 commit intomainfrom
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds new generic numeric APIs to System.Random to generate random integer values for any IBinaryInteger<T> and random IEEE-754 binary floating-point values for any IBinaryFloatingPointIeee754<T>.
Changes:
- Add
Random.NextInteger<T>(),Random.NextInteger<T>(T maxValue), andRandom.NextInteger<T>(T minValue, T maxValue)with fast paths for common integer types and generic rejection-sampling fallbacks. - Add
Random.NextBinaryFloat<T>()with fast paths forfloat/double/NFloatand a generic implementation that guarantees results in[0, 1). - Update reference assembly API surface and add unit tests covering edge cases, large ranges, and derived
Randomdispatch behavior.
Show a summary per file
| File | Description |
|---|---|
src/libraries/System.Private.CoreLib/src/System/Random.cs |
Implements the new generic integer and binary-float generation APIs, including fast paths and fallbacks. |
src/libraries/System.Runtime/ref/System.Runtime.cs |
Exposes the new Random APIs in the System.Runtime reference assembly. |
src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Random.cs |
Adds coverage for argument validation, boundary/range behavior, and derived Random virtual dispatch; also introduces helper numeric wrapper types for custom reference-type coverage. |
Copilot's findings
- Files reviewed: 3/3 changed files
- Comments generated: 3
gfoidl
reviewed
Apr 28, 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.
Adds generic numeric APIs to
System.Random:NextInteger<T>()NextInteger<T>(T maxValue)NextInteger<T>(T minValue, T maxValue)NextBinaryFloat<T>().Fixes #75431