For my programming project I need a function that returns a random variable X of type double in range [1, +infinity>. This variable should follow a simple rule: the higher the value, the lower the probability of it being generated should be. For example, a value of 2.3 should have a much higher probability of being generated than 10.8. Similarly, 10.8 should have a higher probability than 534.1 being returned.
====================================================================
The variable X should follow this distribution:
The probability of X being in range <-infinity, 1> should be 0.
The probability of X being in range [1, +infinity> should be 1.
The probability of X being in range [2, +infinity> should be 0.5.
The probability of X being in range [3, +infinity> should be 0.3333...
The probability of X being in range [4, +infinity> should be 0.25.
...
Generally, the probability of X being in range [x, +infinity] should be 1 / x, where x is any real number in range [1, +infinity>.
How can I achieve such a function in any programming language?
See that the function $f(t) = \frac{1}{t^2}$ over the space of $[1,\infty)$ is indeed a PDF: $$\int_1^\infty \frac{1}{t^2} \, dt = 1$$
Also see that $$\int_X^\infty \frac{1}{t^2} \, dt = \frac{1}{X}$$ for $X \gt 1$ in this case.
Now to find the CDF, notice that $$\int_1^X\frac{1}{t^2} \, dt =\int_1^\infty \frac{1}{t^2} \, dt - \int_X^\infty \frac{1}{t^2} \, dt = \frac{X-1}{X} $$
Using the hint of inverse transform sampling in the link, generate a uniform random variable $u$ between $0$ and $1$ and solve the equation $$u = \frac{X-1}{X} \implies X = \frac{1}{1-u}$$
Therefore, you should generate your random variable, then substitute it into the above equation for $X$, and this $X$ is the sampled random variable that you are seeking from the given distribution