public class RandomSampler extends PersistentObject
Problem: This class solves problems including the following: Suppose we have a file containing 10^12 objects. We would like to take a truly random subset of 10^6 objects and do something with it, for example, compute the sum over some instance field, or whatever. How do we choose the subset? In particular, how do we avoid multiple equal elements? How do we do this quick and without consuming excessive memory? How do we avoid slowly jumping back and forth within the file?
Sorted Simple Random Sample Without Replacement (SRSWOR): What are the exact semantics of this class? What is a SRSWOR? In which sense exactly is a returned set "random"? It is random in the sense, that each number from the N numbers has the same probability to be included in the n chosen numbers. For those who think in implementations rather than abstract interfaces: Suppose, we have an empty list. We pick a random number between 1 and 10^12 and add it to the list only if it was not already picked before, i.e. if it is not already contained in the list. We then do the same thing again and again until we have eventually collected 10^6 distinct numbers. Now we sort the set ascending and return it.
Performance: Space requirements are zero. Running time is O(n) on average, O(N) in the worst case.
n  N  Speed [seconds] 
10^{3}  1.2*10^{3}  0.0014 
10^{3}  10^{7}  0.006 
10^{5}  10^{7}  0.7 
9.0*10^{6}  10^{7}  8.5 
9.9*10^{6}  10^{7}  2.0 (samples more than 95%) 
10^{4}  10^{12}  0.07 
10^{7}  10^{12}  60 
Scalability: This random sampler is designed to be scalable. In iterator style, it is able to compute and deliver sorted random sets stepwise in units called blocks. Example: Computing n=9 random numbers from the interval [1,50] in 3 blocks may yield the blocks (7,13,14), (27,37,42), (45,46,49). (The maximum of a block is guaranteed to be less than the minimum of its successor block. Every block is sorted ascending. No element will ever occur twice, both within a block and among blocks.) A block can be computed and retrieved with method nextBlock. Successive calls to method nextBlock will deliver as many random numbers as required.
Computing and retrieving samples in blocks is useful if you need very many random numbers that cannot be stored in main memory at the same time. For example, if you want to compute 10^10 such numbers you can do this by computing them in blocks of, say, 500 elements each. You then need only space to keep one block of 500 elements (i.e. 4 KB). When you are finished processing the first 500 elements you call nextBlock to fill the next 500 elements into the block, process them, and so on. If you have the time and need, by using such blocks you can compute random sets up to n=10^19 random numbers.
If you do not need the block feature, you can also directly call the static methods of this class without needing to construct a RandomSampler instance first.
Random number generation: By default uses MersenneTwister, a very strong random number generator, much better than java.util.Random. You can also use other strong random number generators of Paul Houle's RngPack package. For example, Ranecu, Ranmar and Ranlux are strong well analyzed research grade pseudorandom number generators with known periods.
Implementation: after J.S. Vitter, An Efficient Algorithm for Sequential Random Sampling, ACM Transactions on Mathematical Software, Vol 13, 1987. Paper available here.
RandomSamplingAssistant
,
Serialized FormserialVersionUID
Constructor and Description 

RandomSampler(long n,
long N,
long low,
RandomEngine randomGenerator)
Constructs a random sampler that computes and delivers sorted random sets in blocks.

Modifier and Type  Method and Description 

Object 
clone()
Returns a deep copy of the receiver.

static void 
main(String[] args)
Tests this class.

void 
nextBlock(int count,
long[] values,
int fromIndex)
Computes the next count random numbers of the sorted random set specified on instance construction
and fills them into values, starting at index fromIndex.

protected static void 
rejectMethodD(long n,
long N,
int count,
long low,
long[] values,
int fromIndex,
RandomEngine randomGenerator)
Efficiently computes a sorted random set of count elements from the interval [low,low+N1].

static void 
sample(long n,
long N,
int count,
long low,
long[] values,
int fromIndex,
RandomEngine randomGenerator)
Efficiently computes a sorted random set of count elements from the interval [low,low+N1].

protected static void 
sampleMethodA(long n,
long N,
int count,
long low,
long[] values,
int fromIndex,
RandomEngine randomGenerator)
Computes a sorted random set of count elements from the interval [low,low+N1].

protected static void 
sampleMethodD(long n,
long N,
int count,
long low,
long[] values,
int fromIndex,
RandomEngine randomGenerator)
Efficiently computes a sorted random set of count elements from the interval [low,low+N1].

static void 
test(long n,
long N,
long low,
int chunkSize,
int times)
Tests the methods of this class.

protected static void 
testNegAlphaInv(String[] args)
Tests different values for negaalphainv.

public RandomSampler(long n, long N, long low, RandomEngine randomGenerator)
n
 the total number of elements to choose (must be n >= 0 and n <= N).N
 the interval to choose random numbers from is [low,low+N1].low
 the interval to choose random numbers from is [low,low+N1]. Hint: If low==0, then random numbers will be drawn from the interval [0,N1].randomGenerator
 a random number generator. Set this parameter to null to use the default random number generator.public Object clone()
clone
in class PersistentObject
public static void main(String[] args)
public void nextBlock(int count, long[] values, int fromIndex)
Numbers are filled into the specified array starting at index fromIndex to the right. The array is returned sorted ascending in the range filled with numbers.
count
 the number of elements to be filled into values by this call (must be >= 0).values
 the array into which the random numbers are to be filled; must have a length >= count+fromIndex.fromIndex
 the first index within values to be filled with numbers (inclusive).protected static void rejectMethodD(long n, long N, int count, long low, long[] values, int fromIndex, RandomEngine randomGenerator)
Running time is O(count), on average. Space requirements are zero.
Numbers are filled into the specified array starting at index fromIndex to the right. The array is returned sorted ascending in the range filled with numbers.
n
 the total number of elements to choose (must be >= 0).N
 the interval to choose random numbers from is [low,low+N1].count
 the number of elements to be filled into values by this call (must be >= 0 and <=n). Normally, you will set count=n.low
 the interval to choose random numbers from is [low,low+N1]. Hint: If low==0, then draws random numbers from the interval [0,N1].values
 the array into which the random numbers are to be filled; must have a length >= count+fromIndex.fromIndex
 the first index within values to be filled with numbers (inclusive).randomGenerator
 a random number generator.public static void sample(long n, long N, int count, long low, long[] values, int fromIndex, RandomEngine randomGenerator)
Running time is O(count), on average. Space requirements are zero.
Numbers are filled into the specified array starting at index fromIndex to the right. The array is returned sorted ascending in the range filled with numbers.
Random number generation: By default uses MersenneTwister, a very strong random number generator, much better than java.util.Random. You can also use other strong random number generators of Paul Houle's RngPack package. For example, Ranecu, Ranmar and Ranlux are strong well analyzed research grade pseudorandom number generators with known periods.
n
 the total number of elements to choose (must be n >= 0 and n <= N).N
 the interval to choose random numbers from is [low,low+N1].count
 the number of elements to be filled into values by this call (must be >= 0 and <=n). Normally, you will set count=n.low
 the interval to choose random numbers from is [low,low+N1]. Hint: If low==0, then draws random numbers from the interval [0,N1].values
 the array into which the random numbers are to be filled; must have a length >= count+fromIndex.fromIndex
 the first index within values to be filled with numbers (inclusive).randomGenerator
 a random number generator. Set this parameter to null to use the default random number generator.protected static void sampleMethodA(long n, long N, int count, long low, long[] values, int fromIndex, RandomEngine randomGenerator)
Running time is O(N), on average. Space requirements are zero.
Numbers are filled into the specified array starting at index fromIndex to the right. The array is returned sorted ascending in the range filled with numbers.
n
 the total number of elements to choose (must be >= 0).N
 the interval to choose random numbers from is [low,low+N1].count
 the number of elements to be filled into values by this call (must be >= 0 and <=n). Normally, you will set count=n.low
 the interval to choose random numbers from is [low,low+N1]. Hint: If low==0, then draws random numbers from the interval [0,N1].values
 the array into which the random numbers are to be filled; must have a length >= count+fromIndex.fromIndex
 the first index within values to be filled with numbers (inclusive).randomGenerator
 a random number generator.protected static void sampleMethodD(long n, long N, int count, long low, long[] values, int fromIndex, RandomEngine randomGenerator)
Running time is O(count), on average. Space requirements are zero.
Numbers are filled into the specified array starting at index fromIndex to the right. The array is returned sorted ascending in the range filled with numbers.
n
 the total number of elements to choose (must be >= 0).N
 the interval to choose random numbers from is [low,low+N1].count
 the number of elements to be filled into values by this call (must be >= 0 and <=n). Normally, you will set count=n.low
 the interval to choose random numbers from is [low,low+N1]. Hint: If low==0, then draws random numbers from the interval [0,N1].values
 the array into which the random numbers are to be filled; must have a length >= count+fromIndex.fromIndex
 the first index within values to be filled with numbers (inclusive).randomGenerator
 a random number generator.public static void test(long n, long N, long low, int chunkSize, int times)
protected static void testNegAlphaInv(String[] args)
Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.