laur.rand
Class RandomSample
java.lang.Object
|
+--laur.rand.RandomSample
- public class RandomSample
- extends java.lang.Object
This class allows the random selection of n numbers from 1 to N.
The algorithm used here is described in Knuth, TAOCP, volume 2,
second print, section 3.4.2, algorithm S on page 122 (page 142 in
the third edition)
Constructor Summary |
RandomSample(long N,
int n)
Create a new generator of random samples of n distinct
values from the numbers 1 to N . |
RandomSample(long N,
int n,
java.util.Random randgen)
Create a new generator of random samples of n
distinct values from the numbers 1 to N using
randgen as a source of random numbers. |
Method Summary |
static void |
main(java.lang.String[] args)
sample usage and testing |
long[] |
nextSample()
Return a random sample. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
RandomSample
public RandomSample(long N,
int n)
- Create a new generator of random samples of
n
distinct
values from the numbers 1 to N
.
- Parameters:
N
- specifies the range from which we want a sample,
the range will be 1...N
.n
- specifies the size of the sample, the sample will contain
distinct values in the range specified by N
.- Throws:
java.lang.IllegalArgumentException
- thrown if n
or
N
are smaller than 1 or if n
is greater
than N
.
RandomSample
public RandomSample(long N,
int n,
java.util.Random randgen)
- Create a new generator of random samples of
n
distinct values from the numbers 1 to N
using
randgen
as a source of random numbers.
- Parameters:
N
- specifies the range from which we want a sample,
the range will be 1...N
.n
- specifies the size of the sample, the sample will contain
distinct values in the range specified by N
.randgen
- a Random object to be used by the generator.- Throws:
java.lang.IllegalArgumentException
- thrown if n
or
N
are smaller than 1 or if n
is greater
than N
.
nextSample
public long[] nextSample()
- Return a random sample.
- Returns:
- an array of
n
numbers sampled from the
numbers 1...N
.
main
public static void main(java.lang.String[] args)
- sample usage and testing