Package cern.jet.random.sampling
Class RandomSamplingAssistant
- java.lang.Object
-
- cern.colt.PersistentObject
-
- cern.jet.random.sampling.RandomSamplingAssistant
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
public class RandomSamplingAssistant extends PersistentObject
Conveniently computes a stable Simple Random Sample Without Replacement (SRSWOR) subsequence of n elements from a given input sequence of N elements; Example: Computing a sublist of n=3 random elements from a list (1,...,50) may yield the sublist (7,13,47). The subsequence is guaranteed to be stable, i.e. elements never change position relative to each other. Each element from the N elements has the same probability to be included in the n chosen elements. This class is a convenience adapter for RandomSampler using blocks.- Version:
- 1.0, 02/05/99
- See Also:
RandomSampler
, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected long[]
buffer
protected int
bufferPosition
(package private) static int
MAX_BUFFER_SIZE
protected long
n
protected RandomSampler
sampler
protected long
skip
-
Fields inherited from class cern.colt.PersistentObject
serialVersionUID
-
-
Constructor Summary
Constructors Constructor Description RandomSamplingAssistant(long n, long N, RandomEngine randomGenerator)
Constructs a random sampler that samples n random elements from an input sequence of N elements.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Object
clone()
Returns a deep copy of the receiver.protected void
fetchNextBlock()
Not yet commented.RandomEngine
getRandomGenerator()
Returns the used random generator.static void
main(java.lang.String[] args)
Tests random sampling.static int[]
sampleArray(int n, int[] elements)
Just shows how this class can be used; samples n elements from and int[] array.boolean
sampleNextElement()
Returns whether the next element of the input sequence shall be sampled (picked) or not.static void
test(long n, long N)
Tests the methods of this class.static void
testArraySampling(int n, int N)
Tests the methods of this class.private void
xsampleNextElements(BooleanArrayList acceptList)
Returns whether the next elements of the input sequence shall be sampled (picked) or not.
-
-
-
Field Detail
-
sampler
protected RandomSampler sampler
-
buffer
protected long[] buffer
-
bufferPosition
protected int bufferPosition
-
skip
protected long skip
-
n
protected long n
-
MAX_BUFFER_SIZE
static final int MAX_BUFFER_SIZE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
RandomSamplingAssistant
public RandomSamplingAssistant(long n, long N, RandomEngine randomGenerator)
Constructs a random sampler that samples n random elements from an input sequence of N elements.- Parameters:
n
- the total number of elements to choose (must be >= 0).N
- number of elements to choose from (must be >= n).randomGenerator
- a random number generator. Set this parameter to null to use the default random number generator.
-
-
Method Detail
-
clone
public java.lang.Object clone()
Returns a deep copy of the receiver.- Overrides:
clone
in classPersistentObject
- Returns:
- a copy of the receiver.
-
fetchNextBlock
protected void fetchNextBlock()
Not yet commented.
-
getRandomGenerator
public RandomEngine getRandomGenerator()
Returns the used random generator.
-
main
public static void main(java.lang.String[] args)
Tests random sampling.
-
sampleArray
public static int[] sampleArray(int n, int[] elements)
Just shows how this class can be used; samples n elements from and int[] array.
-
sampleNextElement
public boolean sampleNextElement()
Returns whether the next element of the input sequence shall be sampled (picked) or not.- Returns:
- true if the next element shall be sampled (picked), false otherwise.
-
test
public static void test(long n, long N)
Tests the methods of this class. To do benchmarking, comment the lines printing stuff to the console.
-
testArraySampling
public static void testArraySampling(int n, int N)
Tests the methods of this class. To do benchmarking, comment the lines printing stuff to the console.
-
xsampleNextElements
private void xsampleNextElements(BooleanArrayList acceptList)
Returns whether the next elements of the input sequence shall be sampled (picked) or not. one is chosen from the first block, one from the second, ..., one from the last block.- Parameters:
acceptList
- a bitvector which will be filled with true where sampling shall occur and false where it shall not occur.
-
-