1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 00:58:48 +00:00

Remove Random.NextInt64

This functionality is built in starting with .NET 6
This commit is contained in:
Oliver Booth 2022-04-20 17:17:49 +01:00
parent ea08d0fb9e
commit 928fa64ba4
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634

View File

@ -179,87 +179,6 @@ public static class RandomExtensions
return list[random.Next(list.Count)];
}
/// <summary>
/// Returns a random 64-bit integer.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <returns>
/// A 64-bit signed integer greater than or equal to 0 and less than <see cref="long.MaxValue" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static long NextInt64(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
int sample = random.Next();
var sampledRandom = new Random(sample);
return ((long)sample << 32) | (uint)sampledRandom.Next();
}
/// <summary>
/// Returns a non-negative random 64-bit integer that is less than the specified maximum.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <param name="maxValue">The exclusive upper bound of the random number returned.</param>
/// <returns>
/// A 64-bit signed integer that is greater than or equal to 0, and less than <paramref name="maxValue" />; that is,
/// the range of return values ordinarily includes 0 but not <paramref name="maxValue" />. However, if
/// <paramref name="maxValue" /> equals 0, <paramref name="maxValue" /> is returned.
/// </returns>
/// <remarks><paramref name="maxValue" /> must be greater than or equal to 0.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="maxValue" /> is less than 0.</exception>
public static long NextInt64(this Random random, long maxValue)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
if (maxValue < 0)
{
throw new ArgumentOutOfRangeException(nameof(maxValue), ExceptionMessages.MaxValueGreaterThanEqualTo0);
}
return random.NextInt64(0, maxValue);
}
/// <summary>
/// Returns a random 64-bit integer that is within a specified range.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <param name="minValue">The inclusive lower bound of the random number returned.</param>
/// <param name="maxValue">The exclusive upper bound of the random number returned.</param>
/// <returns>
/// A 64-bit signed integer greater than or equal to <paramref name="minValue" /> and less than
/// <paramref name="maxValue" />; that is, the range of return values includes <paramref name="minValue" /> but not
/// <paramref name="maxValue" />. If <paramref name="minValue" /> equals <paramref name="maxValue" />,
/// <paramref name="minValue" /> is returned.
/// </returns>
/// <remarks><paramref name="maxValue" /> must be greater than or equal to <paramref name="minValue" />.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">
/// <paramref name="maxValue" /> is less than <paramref name="minValue" />.
/// </exception>
public static long NextInt64(this Random random, long minValue, long maxValue)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
if (maxValue < minValue)
{
throw new ArgumentOutOfRangeException(nameof(maxValue), ExceptionMessages.MaxValueGreaterThanEqualToMinValue);
}
long sample = random.NextInt64();
return (sample % (maxValue - minValue)) + minValue;
}
/// <summary>
/// Returns a random single-precision floating point number between 0 and 1.
/// </summary>