1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 19:18:46 +00:00

(#37) Add Random.NextInt64

Uses an internal Random instance to generate a second sample which is
OR'd with the first to produce a 64-bit integer
This commit is contained in:
Oliver Booth 2021-03-09 13:18:12 +00:00
parent 9d0c300250
commit 8a168f0ff0

View File

@ -163,6 +163,87 @@ namespace X10D.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));
}
var sample = random.Next();
var sampledRandom = new Random(sample);
return (long)sample << 32 | 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(ExceptionMessages.MaxValueGreaterThanEqualToMinValue);
}
var sample = random.NextInt64();
return (sample % (maxValue - minValue)) + minValue;
}
/// <summary>
/// Returns a random single-precision floating point number between 0 and 1.
/// </summary>