-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement RandomSamplingUtils.randomSelection (#40)
- Loading branch information
1 parent
b2e06a4
commit 14f021e
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/test/java/gr/james/sampling/RandomSamplingUtilsRandomSelectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package gr.james.sampling; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Tests for {@link RandomSamplingUtils#randomSelection(int, int, Random)}. | ||
*/ | ||
public class RandomSamplingUtilsRandomSelectionTest { | ||
/** | ||
* Check correctness for n=6 and k=3. The total number of 3-tuples should be 60 with equal probability of inclusion. | ||
*/ | ||
@Test | ||
public void correctness() { | ||
final int REPS = 200000000; | ||
final Random rng = new Random(); | ||
final Map<List<Integer>, Long> frequencies = new HashMap<>(); | ||
for (int i = 0; i < REPS; i++) { | ||
final int[] a = RandomSamplingUtils.randomSelection(5, 3, rng); | ||
final List<Integer> aList = Arrays.stream(a).boxed().collect(Collectors.toList()); | ||
frequencies.put(aList, frequencies.getOrDefault(aList, 0L) + 1); | ||
} | ||
Assert.assertEquals(60, frequencies.size()); | ||
final long firstFrequency = frequencies.values().iterator().next(); | ||
for (long v : frequencies.values()) { | ||
Assert.assertEquals(1.0, (double) firstFrequency / v, 1.0e-2); | ||
} | ||
} | ||
} |