Problem description:
Describes an implementation of the random (a, B) process. It only calls random (0, 1 ). As a function of A and B, yourProgramWhat is the expected running time?
This isAlgorithmAn introduction question.
There is no fixed answer. After reading a lot of online solutions, I will summarize my ideas. First, random (0, 1) is a random generation of two integers, 1. Random (A, B) is a random generation of an integer between [a, B. Random means equal probability. Generating Random (A, B) is equivalent to randomly generating a number in [0, B-A], and then adding a to the number between [a, B.
Solution 1:
If K is set to B-A, assume that the power of C of 2 (expressed by 2 ^ C below) is the first positive integer power of 2 greater than B-A, then call random (0, 1) c, generate a C-bit binary number, corresponding to a [0, 2 ^ C-1] decimal number, if this number is within [0, B-A], output, otherwise, the C-bit binary number is generated until the number within [0, B-A] is obtained.
Below is the JavaCode:
Public Int Randomab2 ( Int A, Int B ){ Int K = B- A; // Find the N of the first 2 ^ n greater than K Int K1 = K; Int C = 0 ; While (K1! = 0 ) {C ++ ; K1 = ( Int ) Math. Floor (K1/2 );} Int S = 0 ; // Generate numbers from 0 to K Do {S = 0 ; For ( Int I = 0; I <C; I ++ ) {S + = Random01 () * Math. Pow (2 , I );}} While (S> K ); Return S + A ;}
Expected running time:
The number of running times of the second while loop is geometrically distributed, and each success (that is, the number within [0, B-A] is obtained) the probability is --- P = (B-A + 1)/(2 ^ C), Here C should be lg (B-a) rounded up, therefore, the expected number of running times is tavg = 1/p.
Expected runtime T (a, B) = tavg * lg (B-a) * D. LG (B-a) is the logarithm of B-A based on 2, and D is the time when the code runs in the second while loop, and is a constant. This seems to be an O (lg (B-a) level running time.
Solution 2:
This method is from the answer to http://blog.csdn.net/longhuihu/article/details/5864442. This method is also a binary sequence whose length is k = B-A + 1 --- R1, R2 ,.., delete the number in [0, B-A] corresponding to 0 in the binary sequence to reduce the range of the number to be generated.
The following is the Java code used to simulate this process:
View code
Public Int Randomonoclonal ( Int A, Int B ){ Int K = B-A + 1 ; Arraylist <Integer> S = New Arraylist <integer> (); Arraylist <Integer> Tems; For ( Int I = 0; I <K; I ++ ) {S. Add (I);} tems = Copy (s ); While (S. Size ()> 1 ){ Int N =S. Size (); For ( Int J = n-1; j> = 0; j -- ){ Int R = Random01 (); If (R = 0 ) {Tems. Remove (j );}} If (TEMS. isempty () {tems = Copy (s );} Else {S = Tems; tems = Copy (s );}} Return S. Get (0) + A ;}
This is easy to understand, but the expected running time of the analysis is complicated.
First, how many times does B-A + 1 Length binary sequence need to be generated to reduce the range? This is also a geometric distribution. The probability of success in each test is p = 1-2/2 ^ K (as long as the test fails to generate full 0 or full 1 ). The expected number of tests is tavg = 1/p. The running time t (A, B) can be expressed as T (a, B) = tavg * K * D + T (X ). This T (x) represents any possible narrow range after which X is between 1-(k-1) and D is the constant running time in the loop.
This articleArticleIt proves that the number between A and B in this way is definitely equal probability. The mathematical induction method is applied to prove the method as follows:
(1) equi probability generation 0, 1
(2) assume that there are random (M) and M <k probabilities to generate a number between 0 and M.
(3) Proof that the number of [0, k-1] generated by random (k) is also Equal Probability:
Define events:
AI -- number of output I
Bim-the number of MB after a round of filtering is left, including the number of I
P (ai | BIM) = random (m) = 1/m, which is the assumption of (2.
P (AI, BIM) = P (BIM) * P (ai | BIM) = P (BIM) * (1/M ).
P (BIM) = C (k-1), m-1)/(2k-2 ). C (k-1), (m-1) is a combination formula.
P (AI) = Sigma P (AI, BIM) for M = 1 :( k-1 ). This is a summation formula for m from 1 to K-1 values.
P (AI) = 1/K.