問題:如何隨機從n個對象中選擇一個對象,這n個對象是按序排列的,但是在此之前你是不知道n的值的。
思路:如果我們知道n的值,那麼問題就可以簡單的用一個大隨機數rand()%n得到一個確切的隨機位置,那麼該位置的對象就是所求的對象,選中的概率是1/n。
但現在我們並不知道n的值,這個問題便抽象爲蓄水池抽樣問題,即從一個包含n個對象的列表S中隨機選取k個對象,n爲一個非常大或者不知道的值。通常情況下,n是一個非常大的值,大到無法一次性把所有列表S中的對象都放到內存中。我們這個問題是蓄水池抽樣問題的一個特例,即k=1。
解法:我們總是選擇第一個對象,以1/2的概率選擇第二個,以1/3的概率選擇第三個,以此類推,以1/m的概率選擇第m個對象。當該過程結束時,每一個對象具有相同的選中概率,即1/n,證明如下。
證明:第m個對象最終被選中的概率P=選擇m的概率*其後面所有對象不被選擇的概率,即
對應的該問題的僞代碼如下:
i = 0
while more input items
with probability 1.0 / ++i
choice = this input item
print choice
C++代碼實現如下:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;
typedef vector<int> IntVec;
typedef typename IntVec::iterator Iter;
typedef typename IntVec::const_iterator Const_Iter;
// generate a random number between i and k,
// both i and k are inclusive.
int randint(int i, int k)
{
if (i > k)
{
int t = i; i = k; k = t; // swap
}
int ret = i + rand() % (k - i + 1);
return ret;
}
// take 1 sample to result from input of unknown n items.
bool reservoir_sampling(const IntVec &input, int &result)
{
srand(time(NULL));
if (input.size() <= 0)
return false;
Const_Iter iter = input.begin();
result = *iter++;
for (int i = 1; iter != input.end(); ++iter, ++i)
{
int j = randint(0, i);
if (j < 1)
result = *iter;
}
return true;
}
int main()
{
const int n = 10;
IntVec input(n);
int result = 0;
for (int i = 0; i != n; ++i)
input[i] = i;
if (reservoir_sampling(input, result))
cout << result << endl;
return 0;
}
對應蓄水池抽樣問題,可以類似的思路解決。先把讀到的前k個對象放入“水庫”,對於第k+1個對象開始,以k/(k+1)的概率選擇該對象,以k/(k+2)的概率選擇第k+2個對象,以此類推,以k/m的概率選擇第m個對象(m>k)。如果m被選中,則隨機替換水庫中的一個對象。最終每個對象被選中的概率均爲k/n,證明如下。
證明:第m個對象被選中的概率=選擇m的概率*(其後元素不被選擇的概率+其後元素被選擇的概率*不替換第m個對象的概率),即
蓄水池抽樣問題的僞代碼如下:
array S[n]; //source, 0-based
array R[k]; // result, 0-based
integer i, j;
// fill the reservoir array
for each i in 0 to k - 1 do
R[i] = S[i]
done;
// replace elements with gradually decreasing probability
for each i in k to n do
j = random(0, i); // important: inclusive range
if j < k then
R[j] = S[i]
fi
done
C++代碼實現如下,該版本假設n知道,但n非常大:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// generate a random number between i and k,
// both i and k are inclusive.
int randint(int i, int k)
{
if (i > k)
{
int t = i; i = k; k = t; // swap
}
int ret = i + rand() % (k - i + 1);
return ret;
}
// take m samples to result from input of n items.
bool reservoir_sampling(const int *input, int n, int *result, int m)
{
srand(time(NULL));
if (n < m || input == NULL || result == NULL)
return false;
for (int i = 0; i != m; ++i)
result[i] = input[i];
for (int i = m; i != n; ++i)
{
int j = randint(0, i);
if (j < m)
result[j] = input[i];
}
return true;
}
int main()
{
const int n = 100;
const int m = 10;
int input[n];
int result[m];
for (int i = 0; i != n; ++i)
input[i] = i;
if (reservoir_sampling(input, n, result, m))
for (int i = 0; i != m; ++i)
cout << result[i] << " ";
cout << endl;
return 0;
}
下面這個程序假設不知道n的大小
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;
typedef vector<int> IntVec;
typedef typename IntVec::iterator Iter;
typedef typename IntVec::const_iterator Const_Iter;
// generate a random number between i and k,
// both i and k are inclusive.
int randint(int i, int k)
{
if (i > k)
{
int t = i; i = k; k = t; // swap
}
int ret = i + rand() % (k - i + 1);
return ret;
}
// take m samples to result from input of n items.
bool reservoir_sampling(const IntVec &input, IntVec &result, int m)
{
srand(time(NULL));
if (input.size() < m)
return false;
result.resize(m);
Const_Iter iter = input.begin();
for (int i = 0; i != m; ++i)
result[i] = *iter++;
for (int i = m; iter != input.end(); ++i, ++iter)
{
int j = randint(0, i);
if (j < m)
result[j] = *iter;
}
return true;
}
int main()
{
const int n = 100;
const int m = 10;
IntVec input(n), result(m);
for (int i = 0; i != n; ++i)
input[i] = i;
if (reservoir_sampling(input, result, m))
for (int i = 0; i != m; ++i)
cout << result[i] << " ";
cout << endl;
return 0;
}
本文參考:
http://www.cnblogs.com/HappyAngel/archive/2011/02/07/1949762.html
http://en.wikipedia.org/wiki/Reservoir_sampling
http://en.wikipedia.org/wiki/Fisher-Yates_shuffle