Given an unknown length list, return a random item in it by scanning it only 1 time

前端 未结 4 636
北海茫月
北海茫月 2021-01-23 05:37

Given an unknown length list, return a random item in it by scanning it only 1 time.

My idea:

A similar algorithm is Reservoir Sampling (posted by others). But

4条回答
  •  清酒与你
    2021-01-23 06:25

    You use reservoir sampling.

    This is not too complicated nor expensive; it is the minimal approach given the constraints that you have (selecting an element from a stream).

    It works just fine if you want a random sample size of 1 and if all the elements have the same weight.

    When you've simplified the code with a k of 1 and no explicit weighting, its still reservoir sampling.

    Not all pseudo random number generators run at the same speed; pick a fast one.


    Comments ask what would happen if you re-used the same random number rather than generating a new random number each step:

    The Wikipedia link given shows the equivalence to the Yates-Fisher/Knuth shuffle. If you asked what would picking the same random number each step of the shuffle would be, you'd be barking.

提交回复
热议问题