TITLE: Expected value uniform decreasing function
QUESTION [6 upvotes]: We are given a function $f(n,k)$ as
for(i=0;i < k;i++)
  n = rand(n);
return n;

rand is defined as a random number generator that uniformly generates values in the range $[0,n)$. It returns a value strictly less than $n$; also $\operatorname{rand}(0)=0$.
What is the expected value of our function $f(n,k)$ given $n$ and $k$?

REPLY [2 votes]: @joriki: how would you account for non integral values of expectation for next iteration (under assumption of function being integral)? Eg, $f(3,2)$, the only non-zero chain is $3 \rightarrow 2 \rightarrow 1$, and this happens with probability $\frac{1}{3}*\frac{1}{2}=\frac{1}{6}$, hence expected value is $\frac{1}{6}$. Whereas your analysis gives value 0.
PS: is it possible to move this under comments section joriki's answer? I don't think I've enough reputation to do that myself.
EDIT
I found the same question here (same question, such small time gap O.o)
The answer by @coffeemath gives out the correct expectation; it is based on the simple recurrence relation $f(x,y)=\frac{1}{x}\sum\limits_{i=0}^{x-1} f(i,y-1)$. Both the analysis given above by Sasha and joriki, work correctly on reals; but when it comes to rand(n) being defined $\mathbb{N} \rightarrow \mathbb{N}$ both seem to neglect the fact that output is integer only (try $f(3,2)$ on their answers, expected value should be $\frac{1}{6}$)