This answer refers to a version of the question in which $x$ is sampled by dividing two random numbers.
As mentioned by Rick Decker's answer, given $x$, we can approximate the running time by $O(\max(\log x,1))$. Assuming that rand
returns a random number in $[0,1]$, the running time should be proportional (up to an additive constant) to
$$
\int_0^1 \int_0^1 \max(\log \tfrac{x}{y},0) \, dx \, dy.
$$
Let us start by computing the inner integral:
$$
\int_0^1 \max(\log \tfrac{x}{y},0) \, dx =
\int_y^1 \log \tfrac{x}{y} \, dx = (1-y)\log y + \int_y^1 \log x \, dx = \\
-(1-y)\log y + \left. x(\log x-1) \right|_y^1 = -(1-y)\log y-1-y(\log y-1) = \\
y-\log y-1.
$$
Integrating this over $y$, we get
$$
\int_0^1 (y-\log y-1) \, dy =
\left. \tfrac{1}{2} y^2 - y\log y \right|_0^1 = \frac{1}{2}.
$$
This shows that in this idealized setting, the expected running time is $O(1)$ (rather than infinity, which could also have been the case).
rand()
is bounded by constant number in your case. Then, initialx
also is bounded and answer is simple. Ah, well, there is one case when this program does not halt. $\endgroup$rand()
, so knowledge of any particular programming language is not needed to be able to answer the question. Is this code fragment in C? Not everyone here may know C. We want questions to be language-independent and to be understandable even to people who don't know a particular programming language. Thank you! $\endgroup$