# Expected Value for Random Variable for an Algorithm

Question:

Algorithm WhoGoesFirst(k):

// k >= 1, the die is fair, and all rolls are independent

Andy rolls the die, let a be the result;

Sean rolls the die, let s be the result;

if a > s

then print Andy goes first;

return k

endif;

if a < s

then print Sean goes first;

return k

endif;

if a = s

then WhoGoesFirst(k + 1)

endif

The algorithm WhoGoesFirst(1) is run, i.e., with k = 1. Define the random variable X to be the value of the output of this algorithm.

What is the expected value E(X) of the random variable X?

Attempt:

I'm not sure how to get an output from this algorithm. Wouldn't the result of rolling the die for both of them have a probability of 1/6? How can I come up with a result that says one person has a higher chance of a roll then the other? And if I assume it is the same, then the recursive function would just loop through the algorithm again. I'm struggling to come up with any output from this algorithm to use to calculate the expected value.

In the first try, with probability 5/6 one is going to roll higher (they roll different) and with probability 1/6 they'll roll the same and the k is going to increase.

In the next roll, k is going to increase by one so the next who goes next algorithm has expected value of the previous one + 1. Using this we can write

E(X) = 5/6 * 1 + 1/6 (1 + E(X))

solving this yields to 6/5

Alternatively, with probability 1/6 we go to the next roll. Probability that k = 1 is 5/6.

p(k = 2) is 1/6 * 5/6 (first one drew, second one different)

p(k = n) is (1/6) ^ (n-1) * 5/6 (first n-1 drew, last one different)

If you sum the infinite series of k*p(k), you'll get the same result as above.

Hope it helps