Rejection sampling with uniform random variables

To simulate $\v U\sim U([0,1]^d)$, we take $\v U = (\bar U_1, \bar U_2, \ldots, \bar U_d)$ where $\bar U_i \sim U[0,1]$ are independent.

For some $A\subseteq [0,1]^d$, to simulate a random variable $\v X$ in $[0,1]^d$ having density $$ \begin{align*} f(\v x) = \frac{1_A(\v x)}{|A|}, \end{align*} $$ take $\v U_1,\v U_2,\ldots$ independent $U([0,1]^d)$ random variables and $\v X$ to be the first of those which land in $A$, i.e. reject the others: $$ \begin{align*} \v X = \v U_N, \quad N=\min\sb{n\geq 1}{\v U_n \in A}. \end{align*} $$ (Derivation)

In general to simulate $\v X$ having a bounded density $f$ in $[0,1]^d$, let $f(\v x)\leq \lambda$ for all $\v x$. We'll apply the same technique as above to $[0,1]^{d+1}$:

Take $\v U_1, \v U_2, \ldots \sim U([0,1]^d)$ and $V_1,V_2,\ldots\sim U[0,1]$ all independent random variables, and $\v X$ to be the the first $\v U_n$ such that $V_n\leq f(\v U_n)/\lambda$: $$ \begin{align*} \v X = \v U_n, \quad N = \min\sb{n\geq 1}{V_n \leq f(\v U_n)/\lambda}. \end{align*} $$ (Derivation)

https://www.geogebra.org/calculator/gtjdw2kc

See the general formulation.