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
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:
- How can I pick a good web cam?Best solution by forbes.com
- How Can You Send A Picture Through An Email To A Phone?Best solution by Yahoo! Answers
- How can you heal a cut without having it leave a mark?Best solution by Yahoo! Answers
- How can I block a number on my cell phone?Best solution by eHow old
- How can i track a number?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.