How to arrange a set of number?

Mathematical Puzzles: How can someone pick a number with uniform probability from a large set of numbers, given a small random number generator? [note: see details]

  • Given  a large set of numbers S={1,⋯,n}S={1,⋯,n}S =\left \{ 1, \cdots , n  \right \} and a uniform random generator that picks numbers from a  smaller set {1,⋯,m}{1,⋯,m}\{ 1, \cdots , m \}, where m≪nm≪nm \ll n  ; is there a method to pick a number from S with uniform probability  without repetition? For repetition i mean: i can simulate a large dice with a coin (or a generator that picks numbers from set {1,2}{1,2}\{ 1,2 \}  ) via binary numbers. I throw the coin k times to create a binary  number of k digits, but with k digits my "virtual" dice can represent  2k2k2^k numbers and can be true that 2k>n2k>n2^k>n, so  if the process simulate a number that is greater than n, i must repeat  it. This is an evolution of this question: http://math.stackexchange.com/questions/273052/pick-a-card-from-a-set-with-the-help-of-a-dice

  • Answer:

    Suppose a∗n=mka∗n=mka*n = m^k for some integers aaa and kkk. Then a/mk=1/na/mk=1/na/m^k = 1/n, so you can generate k rolls and interpret it as a base-m number. If it is less than a, choose 1. If it is less than 2*a, choose 2. Etc. That solves the case where n eventually divides m^k. This happens exactly if every prime factor of n is also a prime factor of m. If this isn't the case, you're out of luck: every scheme involves generating some number of rolls and splitting them up in some way, so we can only achieve probabilities with a power of m in the denominator. But 1/n cannot be written in this way. You can always get arbitrarily close to uniform, though.

Jonathan Paulson at Quora Visit the source

Was this solution helpful to you?

Other answers

After kkk rolls there are going to be mkmkm^k states you could be in.  If mkmkm^k never is a multiple of nnn (as is the case when n has a prime factor that m does not) then it will never be possible to get a truly uniform distribution with a finite number of rolls in the worst case.  However, the expected number of rolls is actually quite reasonable using the below generalized algorithm. base := 1 len := n while base + len > floor(base) + 1 x := Draw from {1, ..., m} uinformly len := len / m base := base + len * (x - 1) output floor(base) The  idea is you start out with the interval [1,n+1)[1,n+1)[1, n+1) and each iteration you  divide it into m equal sections and you choose one.  At every iteration  of the algorithm the overlap with [y,y+1)[y,y+1)[y, y+1) of our interval gives the  relative probability of choosing y.  When the interval is contained  entirely in [y,y+1)[y,y+1)[y, y+1) for some integer y, then we output y. After the first O(logm(n))O(logm(n))O(log_m(n)) iterations of the loop there is always at least a chance of m−1mm−1m\frac{m-1}{m} the loop will terminate each iteration.  Additionally, if mkmkm^k ever is a multiple of nnn then the algorithm definitely will terminate in kkk iterations.

Mark Gordon

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.