[release/9.0-rc2] Fix Random.GetItems breaking change in produced sequence #108018
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.
Backport of #108017 to release/9.0-rc2
/cc @stephentoub
Customer Impact
#92229 added a special-case to optimize Random.GetItems in certain common situations. That, however, ends up observably changing the sequence of values the Random instance produces. If the Random instance was just constructed as
new Random()
orRandom.Shared
, there's no reasonable way for a developer to have taken a dependency on the sequence. But if the instance was constructed with a seed, e.g.new Random(42)
, or if a custom derived type was used, the change is observable, yielding a different sequence of values than previously was produced. Such seeded instances are typically used when it's important that the same sequence always be produced, such as in tests. This fix adds an additional guard clause so that the optimization only kicks in for the cases where it's not observable.Regression
#92229
Testing
Validated that the same sequence produced before is now produced, and also that .NET 8 performance isn't regressed.
Risk
Low. It just means that we no longer employ the .NET 9 optimization in some cases we previously employed it.