*********************************************** Declaration *********************** *******************************
original works, from the "Xiaofeng Moon XJ" blog, Welcome to reprint, please be sure to indicate the source (Http://blog.csdn.net/xiaofengcanyuexj ).
Due to various reasons, there may be many shortcomings, welcome treatise!
*************** ******************************************************************************************
remember in the internship that will, involved in the lottery solution, that is, one day to win the probability of k/n, require a simple and efficient algorithm , where n is only known at the end. in other words, test instructions is how to randomly fetch the number of k from an unknown or a large sample of n space, guaranteeing that each of the probabilities taken is n/k. A good way to solve this problem is the reservoir algorithm reservoir sampling algorithm.
The precondition of application of reservoir algorithm is that the total number of sample space is unknown or large. If the current number of samples is m, each sample is retained in the probability of k/m ... Until the final sample count is N, each of the selected probabilities is k/n, and the flaw is that it is not known which samples are to be selected at the end. So the question is, why not come out at the end of the final result N to randomly extract k samples, the guarantee probability is k/n? In fact, this idea sometimes is not feasible, the data is dynamic growth, the cache system may not be able to store all the sample information, but enough to store the information of k samples, that is, the spatial complexity can be reduced from O (n) to O (k), K is very small relative to N. So the reservoir algorithm is of great application value.
The following is the principle and realization of the reservoir-basin algorithm. First put the number of the first k into the reservoir, to the k+1 sample, the probability of k/(k+1) to change into the reservoir, and then randomly select a sample in the pool as a replacement ..., corresponding to the sample I (I>k), with the probability of k/i left, and then randomly select a replacement from the pool ... This has been done, for arbitrary sample space N, the probability of selecting a single sample is k/n. The certification process is as follows:
This algorithm has a time complexity of O (n), and the spatial complexity is O (k), which can deal with the unknown or large number of sample space, which is a better method. About the reservoir algorithm, in the distributed system has the application, here provides the rich vision the link, the storage pond algorithm expansion.
For work reasons, Java seems to me a bit more important than C + +, all future code as far as possible in Java, directly on the source code.
Import Java.util.random;public class Reservoirsampling {private int m_nchoice;private int [] m_arraychoice;private int m_ Nnownumber;private Random M_rand =new random (System.currenttimemillis ());p ublic reservoirsampling (int k) {m_ Nnownumber = 0;m_nchoice = K;m_arraychoice = new int [k];} public void Adjustchoice () {if (M_nnownumber < M_nchoice) {M_arraychoice [m_nnownumber] = M_nnownumber;} Else{int id = m_rand.nextint (m_nnownumber + 1), if (ID < m_nchoice) {M_arraychoice [id] = M_nnownumber;}} + + M_nnownumber;} public void Showchoice () {for (int i = 0; I <m_arraychoice.length; + + i) {if (I! = 0 && I% = = 0) system.out.p Rintln (); System.out.print (1 + m_arraychoice [i] + "");}} public static void Main (String agr[]) {System.out.println ("My Java program!"); reservoirsampling test = new reservoirsampling, for (int i =0; i <; + + i) {test. Adjustchoice ();} Test. Showchoice ();}}
Because of the time and the level reason, inevitably has the insufficient place, welcome treatise!
Principle and realization of sampling algorithm for reservoir pool