Skip to content

Add generic Random NextInteger/NextBinaryFloat APIs#127462

Merged
stephentoub merged 1 commit intomainfrom
api-proposal/random-next-generic-v2
May 6, 2026
Merged

Add generic Random NextInteger/NextBinaryFloat APIs#127462
stephentoub merged 1 commit intomainfrom
api-proposal/random-next-generic-v2

Conversation

@stephentoub
Copy link
Copy Markdown
Member

@stephentoub stephentoub commented Apr 27, 2026

Adds generic numeric APIs to System.Random:

  • NextInteger<T>()
  • NextInteger<T>(T maxValue)
  • NextInteger<T>(T minValue, T maxValue)
  • NextBinaryFloat<T>().

Fixes #75431

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 27, 2026 17:11
@stephentoub stephentoub changed the title Add generic Random numeric APIs Add generic Random NextInteger/NextBinaryFloat APIs Apr 27, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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), and Random.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 for float/double/NFloat and 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 Random dispatch 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

Comment thread src/libraries/System.Private.CoreLib/src/System/Random.cs
Comment thread src/libraries/System.Private.CoreLib/src/System/Random.cs
Comment thread src/libraries/System.Private.CoreLib/src/System/Random.cs
Comment thread src/libraries/System.Private.CoreLib/src/System/Random.cs
Comment thread src/libraries/System.Private.CoreLib/src/System/Random.cs
Comment thread src/libraries/System.Private.CoreLib/src/System/Random.cs
Comment thread src/libraries/System.Private.CoreLib/src/System/Random.cs
Copy link
Copy Markdown
Member

@tannergooding tannergooding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor nits, but overall looks good.

I think we should remove the Debug.Assert from the public API and validate it properly however, since we'll never trigger that assert ourselves but a user might.

@tannergooding
Copy link
Copy Markdown
Member

@stephentoub I think the only thing that needs resolving here is the need to throw here: #127462 (comment)

The other comments are mainly nits (with one piece of "dead code")

@stephentoub stephentoub merged commit 859a2ea into main May 6, 2026
157 of 159 checks passed
@stephentoub stephentoub deleted the api-proposal/random-next-generic-v2 branch May 6, 2026 02:27
@stephentoub
Copy link
Copy Markdown
Member Author

Grrr. I pushed my branch with changes, but it must have already auto-merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Random.Next<T>

4 participants