What is O value for naive random selection from finite set?

前端 未结 8 1855
终归单人心
终归单人心 2021-02-05 17:25

This question on getting random values from a finite set got me thinking...

It\'s fairly common for people to want to retrieve X unique values from a set of Y values.

8条回答
  •  灰色年华
    2021-02-05 18:20

    If you already have chosen i values then the probability that you pick a new one from a set of y values is

    (y-i)/y.
    

    Hence the expected number of trials to get (i+1)-th element is

    y/(y-i).
    

    Thus the expected number of trials to choose x unique element is the sum

     y/y + y/(y-1) + ... + y/(y-x+1)
    

    This can be expressed using harmonic numbers as

    y (Hy - Hy-x).

    From the wikipedia page you get the approximation

    Hx = ln(x) + gamma + O(1/x)

    Hence the number of necessary trials to pick x unique elements from a set of y elements is

    y (ln(y) - ln(y-x)) + O(y/(y-x)).
    

    If you need then you can get a more precise approximation by using a more precise approximation for Hx. In particular, when x is small it is possible to improve the result a lot.

提交回复
热议问题