Skip to content
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use double.ConvertToIntegerNative where safe to do in System.Random #112046

Merged
merged 1 commit into from
Feb 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,23 @@ public CompatSeedImpl(int seed) =>

public override int Next() => _prng.InternalSample();

public override int Next(int maxValue) => (int)(_prng.Sample() * maxValue);
public override int Next(int maxValue)
{
// We can use ConvertToIntegerNative here since we know the result of
// scaling the sample is in the range [0, int.MaxValue) and therefore
// the integer portion is exactly representable since it's < 2^52.
return double.ConvertToIntegerNative<int>(_prng.Sample() * maxValue);
}

public override int Next(int minValue, int maxValue)
{
// We can use ConvertToIntegerNative here since we know the result of
// scaling the sample is in the range [0, uint.MaxValue) and therefore
// the integer portion is exactly representable since it's < 2^52.
long range = (long)maxValue - minValue;
return range <= int.MaxValue ?
(int)(_prng.Sample() * range) + minValue :
(int)((long)(_prng.GetSampleForLargeRange() * range) + minValue);
double.ConvertToIntegerNative<int>(_prng.Sample() * range) + minValue :
(int)(double.ConvertToIntegerNative<long>(_prng.GetSampleForLargeRange() * range) + minValue);
}

public override long NextInt64()
Expand Down Expand Up @@ -136,17 +145,23 @@ public override int Next()

public override int Next(int maxValue)
{
// We can use ConvertToIntegerNative here since we know the result of
// scaling the sample is in the range [0, int.MaxValue) and therefore
// the integer portion is exactly representable since it's < 2^52.
_prng.EnsureInitialized(_seed);
return (int)(_parent.Sample() * maxValue);
return double.ConvertToIntegerNative<int>(_parent.Sample() * maxValue);
}

public override int Next(int minValue, int maxValue)
{
// We can use ConvertToIntegerNative here since we know the result of
// scaling the sample is in the range [0, uint.MaxValue) and therefore
// the integer portion is exactly representable since it's < 2^52.
_prng.EnsureInitialized(_seed);
long range = (long)maxValue - minValue;
return range <= int.MaxValue ?
(int)(_parent.Sample() * range) + minValue :
(int)((long)(_prng.GetSampleForLargeRange() * range) + minValue);
double.ConvertToIntegerNative<int>(_parent.Sample() * range) + minValue :
(int)(double.ConvertToIntegerNative<long>(_prng.GetSampleForLargeRange() * range) + minValue);
}

public override long NextInt64()
Expand Down
Loading