Skip to content

Commit 4156f7a

Browse files
committed
Lucene.Net.TestFramework: Changed references of RandomGaussian() to use RandomizedTesting.Generators. Also changed incorrect references to NextDouble() to use NextGaussian() where appropriate.
1 parent 6bc648c commit 4156f7a

File tree

3 files changed

+12
-33
lines changed

3 files changed

+12
-33
lines changed

src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs

+9-30
Original file line numberDiff line numberDiff line change
@@ -2849,7 +2849,7 @@ public virtual void AssertDocsSkippingEquals(string info, IndexReader leftReader
28492849
else
28502850
{
28512851
// advance()
2852-
int skip = docid + (int)Math.Ceiling(Math.Abs(skipInterval + Random.NextDouble() * averageGap));
2852+
int skip = docid + (int)Math.Ceiling(Math.Abs(skipInterval + Random.NextGaussian() * averageGap));
28532853
docid = leftDocs.Advance(skip);
28542854
Assert.AreEqual(docid, rightDocs.Advance(skip), info);
28552855
}
@@ -2892,7 +2892,7 @@ public virtual void AssertPositionsSkippingEquals(string info, IndexReader leftR
28922892
else
28932893
{
28942894
// advance()
2895-
int skip = docid + (int)Math.Ceiling(Math.Abs(skipInterval + Random.NextDouble() * averageGap));
2895+
int skip = docid + (int)Math.Ceiling(Math.Abs(skipInterval + Random.NextGaussian() * averageGap));
28962896
docid = leftDocs.Advance(skip);
28972897
Assert.AreEqual(docid, rightDocs.Advance(skip), info);
28982898
}
@@ -3642,15 +3642,12 @@ internal static void LogNativeFSFactoryDebugInfo()
36423642
}
36433643
}
36443644

3645-
private double nextNextGaussian; // LUCENENET specific
3646-
private bool haveNextNextGaussian = false; // LUCENENET specific
3647-
36483645
/// <summary>
36493646
/// Returns the next pseudorandom, Gaussian ("normally") distributed
36503647
/// <c>double</c> value with mean <c>0.0</c> and standard
36513648
/// deviation <c>1.0</c> from this random number generator's sequence.
36523649
/// <para/>
3653-
/// The general contract of <c>nextGaussian</c> is that one
3650+
/// The general contract of <see cref="RandomGaussian()"/> is that one
36543651
/// <see cref="double"/> value, chosen from (approximately) the usual
36553652
/// normal distribution with mean <c>0.0</c> and standard deviation
36563653
/// <c>1.0</c>, is pseudorandomly generated and returned.
@@ -3659,37 +3656,19 @@ internal static void LogNativeFSFactoryDebugInfo()
36593656
/// G. Marsaglia, as described by Donald E. Knuth in <i>The Art of
36603657
/// Computer Programming</i>, Volume 3: <i>Seminumerical Algorithms</i>,
36613658
/// section 3.4.1, subsection C, algorithm P. Note that it generates two
3662-
/// independent values at the cost of only one call to <c>StrictMath.log</c>
3663-
/// and one call to <c>StrictMath.sqrt</c>.
3659+
/// independent values at the cost of only one call to <see cref="Math.Log(double)"/>
3660+
/// and one call to <see cref="Math.Sqrt(double)"/>.
36643661
/// </summary>
36653662
/// <returns>The next pseudorandom, Gaussian ("normally") distributed
36663663
/// <see cref="double"/> value with mean <c>0.0</c> and
36673664
/// standard deviation <c>1.0</c> from this random number
36683665
/// generator's sequence.</returns>
3669-
// LUCENENET specific - moved this here, since this requires instance variables
3670-
// in order to work. Was originally in carrotsearch.randomizedtesting.RandomizedTest.
3666+
// LUCENENET specific - moved this here so we can reference it more readily (similar to how Spatial does it).
3667+
// However, this is also available as an extension method of the System.Random class in RandomizedTesting.Generators.
3668+
// This method was originally in carrotsearch.randomizedtesting.RandomizedTest.
36713669
public double RandomGaussian()
36723670
{
3673-
// See Knuth, ACP, Section 3.4.1 Algorithm C.
3674-
if (haveNextNextGaussian)
3675-
{
3676-
haveNextNextGaussian = false;
3677-
return nextNextGaussian;
3678-
}
3679-
else
3680-
{
3681-
double v1, v2, s;
3682-
do
3683-
{
3684-
v1 = 2 * Random.NextDouble() - 1; // between -1 and 1
3685-
v2 = 2 * Random.NextDouble() - 1; // between -1 and 1
3686-
s = v1 * v1 + v2 * v2;
3687-
} while (s >= 1 || s == 0);
3688-
double multiplier = Math.Sqrt(-2 * Math.Log(s) / s);
3689-
nextNextGaussian = v2 * multiplier;
3690-
haveNextNextGaussian = true;
3691-
return v1 * multiplier;
3692-
}
3671+
return Random.NextGaussian();
36933672
}
36943673
}
36953674

src/Lucene.Net.TestFramework/Util/TestUtil.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ public static string RandomAnalysisString(Random random, int maxLength, bool sim
963963
int wordLength = -1;
964964
while (wordLength < 0)
965965
{
966-
wordLength = (int)(random.NextDouble() * 3 + avgWordLength);
966+
wordLength = (int)(random.NextGaussian() * 3 + avgWordLength);
967967
}
968968
wordLength = Math.Min(wordLength, maxLength - sb.Length);
969969
sb.Append(RandomSubString(random, wordLength, simple));

src/Lucene.Net.Tests/Codecs/Lucene41/TestBlockPostingsFormat3.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ public virtual void AssertDocsSkipping(int docFreq, DocsEnum leftDocs, DocsEnum
469469
else
470470
{
471471
// advance()
472-
int skip = docid + (int)Math.Ceiling(Math.Abs(skipInterval + Random.NextDouble() * averageGap));
472+
int skip = docid + (int)Math.Ceiling(Math.Abs(skipInterval + Random.NextGaussian() * averageGap));
473473
docid = leftDocs.Advance(skip);
474474
Assert.AreEqual(docid, rightDocs.Advance(skip));
475475
}
@@ -509,7 +509,7 @@ public virtual void AssertPositionsSkipping(int docFreq, DocsAndPositionsEnum le
509509
else
510510
{
511511
// advance()
512-
int skip = docid + (int)Math.Ceiling(Math.Abs(skipInterval + Random.NextDouble() * averageGap));
512+
int skip = docid + (int)Math.Ceiling(Math.Abs(skipInterval + Random.NextGaussian() * averageGap));
513513
docid = leftDocs.Advance(skip);
514514
Assert.AreEqual(docid, rightDocs.Advance(skip));
515515
}

0 commit comments

Comments
 (0)