Anda di halaman 1dari 4

Random Number Generator

Martin Bajzek
August 18, 2016

1 Problem
Sabya is writing code to output a random number between 1 and N inclusive. The code
can output different integers in the interval with different probabilities so that the sum of
the probabilities is equal to 1. For evaluation, the code is run twice. If the output in both
cases are different, his score is equal to the sum of the outputs. Otherwise, his score is equal
to 0. Sabya is very intelligent, and he writes code that maximizes his expected score. Find
the expected score. Output the ratio of the expected score to the maximum possible score
2N 1.

2 Solution
Let X denote the random variable of the score. Let p : {1, 2, , N } 7 [0, 1] be the
probability density of the RNG. Then expectancy of X is given by
N
X N
X
E[X] = p(x)p(y)(x + y)
x=1 y=1,y6=x
N X
X N N
X
= p(x)p(y)(x + y) 2 p(x)2 x
x=1 y=1 x=1
N
X
=2 p(x)(1 p(x))x .
x=1

Therefore E[X] = 2 U [p] is a functional of p. Suppose it obtains a maximum for some


function p . For some i, j < N let

p (i) , if x = i

p(x) = p (j) + , if x = j

p (x), if x 6= i, j

for small . Let U () := U [p ] U [p]. Then


U [p] = U [p ] + (2p (i) 2 )i + ( 2p (j) 2 )j
U () = (2p (j) + 2 )j + ( 2p (i) + 2 )i .

1
If p maximizes U then for every index i, j

d
U () = 0 .
d =0
Which implies
1
j p (j) i p (i) = (j i), i, j .
2
These equations together with the normalization condition form an N N system. The
solutions of the system are
!
1 1 + N2 HN 1 12 (N 1) 1
p(x) = N (N x)
x 1 + N HN 1 2

where Hn is the n-th harmonic number and H(0) := 0. For N = 1, 2, 3, 4 the probabilities
are done. However, if we calculate the first few probabilities for N > 4, we find that theyre
negative. The last few could be bigger than 1 as well. Thats very well possible since we
didnt require anywhere that 0 p(x) 1. From inspecting the solutions we see that p(x)
is monotonically increasing. This is also intuitively true since best score distribution would
prefer larger numbers. In the solution we can a priori set all the negative probabilities to
0. Let k1 be the cut point such that p(k1 ) < 0 and p(k1 + 1) 0. Then k1 can be calculated
as  
N 2
k1 = N .
1 + N HN 1
For the other probabilities (numbers bigger than k1 ) the same condition for functional max-
imum holds. Now we have an (N k1 ) (N k1 ) system of equations. The solutions
are !
1 1 + N2 (HN 1 Hk1 ) 12 (N 1 k1 ) 1
p(x; k1 ) = N (N x)
x 1 + N (HN 1 Hk1 ) 2
for x > k1 . The second argument indicates that the probabilities of numbers less than k1 are
set to 0. But some of p(x; k1 ), x > k1 could be negative, too. The new cut point is given by
 
N k1 2
k2 = N .
1 + N (HN 1 Hk1 )
Therefore we can form a sequence of cut points (kn )n where k0 = 0 and
 
N kn1 2
kn = N .
1 + N (HN 1 Hkn1 )
Since at least one of the probabilities must be positive to make the sum equal to 1, the
sequence is bounded from above by N 1. It cannot be falling since all the probabilities
p(i; kn ), i kn are equal to 0. Therefore the sequence must be convergent. Let k := lim kn
n
.
Maybe a faster way to find k is to realize that if all the probabilities are positive then
the first one after the cut is positive. In other words we need to find smallest k such that

2
p(k + 1; k) > 0. This is equivalent (in our case) to solving p(k + 1; k) = 0 and taking the
ceiling of the solution.
!
1 1 + N2 (HN 1 Hk ) 21 (N 1 k) 1
N (N k 1) = 0 .
k+1 1 + N (HN 1 Hk ) 2

With a little bit of algebra this yields


(k + 1)(N + 1 + N (HN 1 Hk )) = N (N 1) .
Lets pause here. For large N we see that k = O(N ). := N k. If N is large then

HN 1 Hk '
N

which yields ' 3N . For a better approximation can we can use
 
1
Hn = ln n + + O
n
and Taylor expansion of the logarithm.
    
1
(N + 1 ) N + 1 N ln 1 + N ln 1 = N (N 1)
N N
2
 
(N + 1 ) N + + = N (N 1)
2N

' 2 N.
So a faster converging series for k would be to put
l m
k0 = N 2 N .

In most cases (N < 106 ) it takes less than two iterations to reach the wanted k. Tidying up
the expression for E[X] we get

2(N k 2)2
 
1
E[X] = (N k)(N + k + 1) .
4 HN Hk

This result is exact. A more efficient but less accurate would be to use N k ' 2 N and
HN Hk ' ln(N/k)
!
1
' ln
1 2N
2 2
' + .
N N
Then the expectancy is
E[X] ' 2N 2 N 2N
as intuitively expected.

3
The code in C++ would be
#include <cmath>
#include <c s t d i o >
#include <i o s t r e a m >
using namespace s t d ;

double H ar mo ni c Di ff ( long n , long m) {


double sum = 0 . 0 ;
f o r ( long i = m+1; i <= n ; i ++) {
sum+= 1 . 0 / i ;
}
return sum ;
}

long FindCut ( long n ) {


i f ( n == 1 | | n == 2 | | n == 3 )
return 0 ;
long a = c e i l ( n 2 s q r t ( n ) ) ;
long b = 0 ;
while ( a != b ) {
b = a;
a = f l o o r ( n ( na 2)/(1+n H ar mo ni c Di ff ( n1,a ) ) ) ;
}
return a ;
}

int main ( ) {
long N, c u t ;
double sum , r e s ;
c i n >> N;
c u t = FindCut (N ) ;
/ sum = 0 . 2 5 ( (Nc u t ) (N+c u t +1)2(Ncut 2)(Ncut 2)/ HarmonicDiff (N, c u t ) ) ; /
r e s = ( (Nc u t ) (N+c u t +1)2(Ncut 2)(Ncut 2)/ Ha rm on i cD if f (N, c u t ) ) / ( 8 N4);
p r i n t f ( %.11 f \n , r e s ) ;
return 0 ;
}

Anda mungkin juga menyukai