You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2020/06/30 21:50:44 UTC
[lucenenet] 08/27: Lucene.Net.TestFramework:RandomExtensions: Added
missing overload of NextInt64(long) to choose only max upper bound
This is an automated email from the ASF dual-hosted git repository.
nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit b66279b4112377bc83352e4e883c53e8617d9749
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Sun Jun 28 17:47:52 2020 +0700
Lucene.Net.TestFramework:RandomExtensions: Added missing overload of NextInt64(long) to choose only max upper bound
---
.../Support/RandomExtensions.cs | 31 ++++++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git a/src/Lucene.Net.TestFramework/Support/RandomExtensions.cs b/src/Lucene.Net.TestFramework/Support/RandomExtensions.cs
index 2de8fa8..9fdc366 100644
--- a/src/Lucene.Net.TestFramework/Support/RandomExtensions.cs
+++ b/src/Lucene.Net.TestFramework/Support/RandomExtensions.cs
@@ -4,6 +4,7 @@ using Lucene.Net.Util;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
+using J2N.Numerics;
namespace Lucene.Net
{
@@ -71,6 +72,36 @@ namespace Lucene.Net
}
/// <summary>
+ /// Generates a random <see cref="long"/> between <c>0</c> (inclusive)
+ /// and <paramref name="n"/> (exclusive).
+ /// </summary>
+ /// <param name="random">This <see cref="Random"/>.</param>
+ /// <param name="n">The bound on the random number to be returned. Must be positive.</param>
+ /// <returns>A random <see cref="long"/> between 0 and <paramref name="n"/>-1.</returns>
+ public static long NextInt64(this Random random, long n)
+ {
+ if (n <= 0)
+ {
+ throw new ArgumentOutOfRangeException(nameof(n), $"{n} <= 0: " + n);
+ }
+
+ long value = NextInt64(random);
+ long range = n - 1;
+ if ((n & range) == 0L)
+ {
+ value &= range;
+ }
+ else
+ {
+ for (long u = value.TripleShift(1); u + range - (value = u % n) < 0L;)
+ {
+ u = NextInt64(random).TripleShift(1);
+ }
+ }
+ return value;
+ }
+
+ /// <summary>
/// Generates a random <see cref="float"/>.
/// </summary>
/// <param name="random">This <see cref="Random"/>.</param>