Reservoir Sampling
// An efficient Java program to randomly
// select k items from a stream of items
import java.util.Arrays;
import java.util.Random;
public class ReservoirSampling
{
// A function to randomly select k items from stream[0..n-1].
static void selectKItems(int stream[], int n, int k)
{
int i; // index for elements in stream[]
// reservoir[] is the output array. Initialize it with
// first k elements from stream[]
int reservoir[] = new int[k];
for (i = 0; i < k; i++) {
reservoir[i] = stream[i];
}
Random r = new Random();
// Iterate from the (k+1)th element to nth element
for (; i < n; i++)
{
// Pick a random index from 0 to i.
int j = r.nextInt(i + 1);
// If the randomly picked index is smaller than k,
// then replace the element present at the index
// with new element from stream
if(j < k) {
reservoir[j] = stream[i];
}
}
System.out.println("Following are k randomly selected items");
System.out.println(Arrays.toString(reservoir));
}
//Driver Program to test above method
public static void main(String[] args) {
int stream[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int n = stream.length;
int k = 5;
selectKItems(stream, n, k);
}
}
//This code is contributed by Sumit GhoshHow does it work?
Implementation: Select K Items from A Stream of N element
Interview Questions
面试题:等概率挑出文件中的一行
问题描述
问题解答
面试题:等概率的挑选Google搜索记录日志中的一百万条中文搜索记录
问题描述
问题解答
那在线算法是怎样的?
Reference
Last updated