java - How is Reservoir Sampling being used here? -


i solving question on leetcode, based on concept of reservoir sampling (rs). question is:

given array (that may contain duplicates), randomly output index of given target number. target number guaranteed exist in array. e.g., if input is: {1,2,3,3,3} , target 3, of indices 2, 3 , 4 must returned equal probability. similarly, if target 1, index 0 should returned (obviously probability of 1).

the upvoted code follows:

public class solution {      int[] nums;     random rnd;      public solution(int[] nums) {         this.nums = nums;         this.rnd = new random();     }      public int pick(int target) {         int result = -1;         int count = 0;         (int = 0; < nums.length; i++) {             if (nums[i] != target)                 continue;             if (rnd.nextint(++count) == 0)                 result = i;         }          return result;     } } 

as understand rs, first select, n numbers. then, when select next number, need select probability, 1/(n+1). how does:

rnd.nextint(++count) == 0 

select number probability of 1/(n+1)? understand ++count make n+1, happened numerator? why rnd.nextint() instead of simple 1?

edit: question link here.

okay, have statement rnd.nextint(++count) == 0

the first time hit target, count increments 1 , statement evaluates to

rnd.nextint(1) == 0  

which returns true, because argument 1 exclusive upper bound , possible value may return 0. @ point, current index set result, because not know if there more targets in array.

the second time encounter target, count becomes 2 , statement evaluates to

rnd.nextint(2) == 0  

and 1 returns either 0 or 1. hence, index has 50% chance selected result.

so there 2 known targets in array , both had 50% chance set result. second target had chance obvious, first target can small calculation. had 100% percent chance chosen first time code hit if-statement , second time there 50% chance second target did not become result (leaving 50% chance first target remained result). these chances combined results in

first target result = 100% x 50% = 50% 

these steps can repeated more 2 targets, makes explanation lot more complex. in end, n targets in array have 1/n chance of being result.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -