Skip to content

Commit

Permalink
Lucene.Net.TestFramework: Changed references of RandomGaussian() to u…
Browse files Browse the repository at this point in the history
…se RandomizedTesting.Generators. Also changed incorrect references to NextDouble() to use NextGaussian() where appropriate.
  • Loading branch information
NightOwl888 committed Apr 21, 2021
1 parent 6bc648c commit 4156f7a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 33 deletions.
39 changes: 9 additions & 30 deletions src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2849,7 +2849,7 @@ public virtual void AssertDocsSkippingEquals(string info, IndexReader leftReader
else
{
// advance()
int skip = docid + (int)Math.Ceiling(Math.Abs(skipInterval + Random.NextDouble() * averageGap));
int skip = docid + (int)Math.Ceiling(Math.Abs(skipInterval + Random.NextGaussian() * averageGap));
docid = leftDocs.Advance(skip);
Assert.AreEqual(docid, rightDocs.Advance(skip), info);
}
Expand Down Expand Up @@ -2892,7 +2892,7 @@ public virtual void AssertPositionsSkippingEquals(string info, IndexReader leftR
else
{
// advance()
int skip = docid + (int)Math.Ceiling(Math.Abs(skipInterval + Random.NextDouble() * averageGap));
int skip = docid + (int)Math.Ceiling(Math.Abs(skipInterval + Random.NextGaussian() * averageGap));
docid = leftDocs.Advance(skip);
Assert.AreEqual(docid, rightDocs.Advance(skip), info);
}
Expand Down Expand Up @@ -3642,15 +3642,12 @@ internal static void LogNativeFSFactoryDebugInfo()
}
}

private double nextNextGaussian; // LUCENENET specific
private bool haveNextNextGaussian = false; // LUCENENET specific

/// <summary>
/// Returns the next pseudorandom, Gaussian ("normally") distributed
/// <c>double</c> value with mean <c>0.0</c> and standard
/// deviation <c>1.0</c> from this random number generator's sequence.
/// <para/>
/// The general contract of <c>nextGaussian</c> is that one
/// The general contract of <see cref="RandomGaussian()"/> is that one
/// <see cref="double"/> value, chosen from (approximately) the usual
/// normal distribution with mean <c>0.0</c> and standard deviation
/// <c>1.0</c>, is pseudorandomly generated and returned.
Expand All @@ -3659,37 +3656,19 @@ internal static void LogNativeFSFactoryDebugInfo()
/// G. Marsaglia, as described by Donald E. Knuth in <i>The Art of
/// Computer Programming</i>, Volume 3: <i>Seminumerical Algorithms</i>,
/// section 3.4.1, subsection C, algorithm P. Note that it generates two
/// independent values at the cost of only one call to <c>StrictMath.log</c>
/// and one call to <c>StrictMath.sqrt</c>.
/// independent values at the cost of only one call to <see cref="Math.Log(double)"/>
/// and one call to <see cref="Math.Sqrt(double)"/>.
/// </summary>
/// <returns>The next pseudorandom, Gaussian ("normally") distributed
/// <see cref="double"/> value with mean <c>0.0</c> and
/// standard deviation <c>1.0</c> from this random number
/// generator's sequence.</returns>
// LUCENENET specific - moved this here, since this requires instance variables
// in order to work. Was originally in carrotsearch.randomizedtesting.RandomizedTest.
// LUCENENET specific - moved this here so we can reference it more readily (similar to how Spatial does it).
// However, this is also available as an extension method of the System.Random class in RandomizedTesting.Generators.
// This method was originally in carrotsearch.randomizedtesting.RandomizedTest.
public double RandomGaussian()
{
// See Knuth, ACP, Section 3.4.1 Algorithm C.
if (haveNextNextGaussian)
{
haveNextNextGaussian = false;
return nextNextGaussian;
}
else
{
double v1, v2, s;
do
{
v1 = 2 * Random.NextDouble() - 1; // between -1 and 1
v2 = 2 * Random.NextDouble() - 1; // between -1 and 1
s = v1 * v1 + v2 * v2;
} while (s >= 1 || s == 0);
double multiplier = Math.Sqrt(-2 * Math.Log(s) / s);
nextNextGaussian = v2 * multiplier;
haveNextNextGaussian = true;
return v1 * multiplier;
}
return Random.NextGaussian();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.TestFramework/Util/TestUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ public static string RandomAnalysisString(Random random, int maxLength, bool sim
int wordLength = -1;
while (wordLength < 0)
{
wordLength = (int)(random.NextDouble() * 3 + avgWordLength);
wordLength = (int)(random.NextGaussian() * 3 + avgWordLength);
}
wordLength = Math.Min(wordLength, maxLength - sb.Length);
sb.Append(RandomSubString(random, wordLength, simple));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ public virtual void AssertDocsSkipping(int docFreq, DocsEnum leftDocs, DocsEnum
else
{
// advance()
int skip = docid + (int)Math.Ceiling(Math.Abs(skipInterval + Random.NextDouble() * averageGap));
int skip = docid + (int)Math.Ceiling(Math.Abs(skipInterval + Random.NextGaussian() * averageGap));
docid = leftDocs.Advance(skip);
Assert.AreEqual(docid, rightDocs.Advance(skip));
}
Expand Down Expand Up @@ -509,7 +509,7 @@ public virtual void AssertPositionsSkipping(int docFreq, DocsAndPositionsEnum le
else
{
// advance()
int skip = docid + (int)Math.Ceiling(Math.Abs(skipInterval + Random.NextDouble() * averageGap));
int skip = docid + (int)Math.Ceiling(Math.Abs(skipInterval + Random.NextGaussian() * averageGap));
docid = leftDocs.Advance(skip);
Assert.AreEqual(docid, rightDocs.Advance(skip));
}
Expand Down

0 comments on commit 4156f7a

Please sign in to comment.