All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cern.jet.random.sampling.RandomSamplingAssistant Maven / Gradle / Ivy

/*
Copyright (c) 1999 CERN - European Organization for Nuclear Research.
Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
is hereby granted without fee, provided that the above copyright notice appear in all copies and 
that both that copyright notice and this permission notice appear in supporting documentation. 
CERN makes no representations about the suitability of this software for any purpose. 
It is provided "as is" without expressed or implied warranty.
*/
package cern.jet.random.sampling;

import cern.colt.list.BooleanArrayList;
import cern.jet.random.engine.RandomEngine;
/**
 * 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.
 *
 * @see RandomSampler
 * @author  [email protected]
 * @version 1.0, 02/05/99
 */
public class RandomSamplingAssistant extends cern.colt.PersistentObject {
//public class RandomSamplingAssistant extends Object implements java.io.Serializable {
	protected RandomSampler sampler;
	protected long[] buffer;
	protected int bufferPosition;

	protected long skip;
	protected long n;

	static final int MAX_BUFFER_SIZE = 200;
/**
 * Constructs a random sampler that samples n random elements from an input sequence of N elements.
 *
 * @param n the total number of elements to choose (must be >= 0).
 * @param N number of elements to choose from (must be >= n).
 * @param randomGenerator a random number generator. Set this parameter to null to use the default random number generator.
 */
public RandomSamplingAssistant(long n, long N, RandomEngine randomGenerator) {
	this.n = n;
	this.sampler = new RandomSampler(n, N, 0, randomGenerator);
	this.buffer = new long[(int)Math.min(n,MAX_BUFFER_SIZE)];
	if (n>0) this.buffer[0] = -1; // start with the right offset
	
	fetchNextBlock();
}
/**
 * Returns a deep copy of the receiver.
 */
public Object clone() {
	RandomSamplingAssistant copy = (RandomSamplingAssistant) super.clone();
	copy.sampler = (RandomSampler) this.sampler.clone();
	return copy;
}
/**
 * Not yet commented.
 */
protected void fetchNextBlock() {
	if (n>0) {
		long last = buffer[bufferPosition];
		sampler.nextBlock((int)Math.min(n,MAX_BUFFER_SIZE), buffer, 0);
		skip = buffer[0] - last - 1;
		bufferPosition=0;
	}
}
/**
 * Returns the used random generator.
 */
public RandomEngine getRandomGenerator() {
	return this.sampler.my_RandomGenerator;
}
/**
 * Tests random sampling.
 */
public static void main(String args[]) {
	long n = Long.parseLong(args[0]);
	long N = Long.parseLong(args[1]);
	//test(n,N);
	testArraySampling((int)n,(int)N);
}
/**
 * Just shows how this class can be used; samples n elements from and int[] array.
 */
public static int[] sampleArray(int n, int[] elements) {
	RandomSamplingAssistant assistant = new RandomSamplingAssistant(n, elements.length, null);
	int[] sample = new int[n];
	int j=0;
	int length = elements.length;
	for (int i=0; itrue if the next element shall be sampled (picked), false otherwise.
 */
public boolean sampleNextElement() {
	if (n==0) return false; //reject
	if (skip-- > 0) return false; //reject

	//accept
	n--;
	if (bufferPosition < buffer.length-1) {
		skip = buffer[bufferPosition+1] - buffer[bufferPosition++];
		--skip;
	}
	else {
		fetchNextBlock();
	}
		
	return true;
}
/**
 * Tests the methods of this class.
 * To do benchmarking, comment the lines printing stuff to the console.
 */
public static void test(long n, long N) {
	RandomSamplingAssistant assistant = new RandomSamplingAssistant(n,N,null);

	cern.colt.list.LongArrayList sample = new cern.colt.list.LongArrayList((int)n);
	cern.colt.Timer timer = new cern.colt.Timer().start();

	for (long i=0; itrue where sampling shall occur and false where it shall not occur.
 */
private void xsampleNextElements(BooleanArrayList acceptList) {
	// manually inlined
	int length = acceptList.size();
	boolean[] accept = acceptList.elements();
	for (int i=0; i 0) {
			accept[i] = false;
			continue;
		} //reject

		//accept
		n--;
		if (bufferPosition < buffer.length-1) {
			skip = buffer[bufferPosition+1] - buffer[bufferPosition++];
			--skip;
		}
		else {
			fetchNextBlock();
		}
			
		accept[i] = true;
	}
}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy