Consider
$ALG$:
Pick an index $i$ at uniformly random.
If $A[i]=X$ then return $i$ else return $FAIL$
We see that $Pr[SUCCESS] = \frac{2}{3}$ i.e., $Pr[FAIL] = \frac{1}{3}$ in a single run.
Suppose we want to boost the success of finding at least one $i$ so that $A[i]=X$ to a probability of $\geq 1-\delta$
We can do so by repeating $ALG$ independently upto a sufficient number of times. This repeated algorithm fails only if all the runs return $FAIL$ otherwise we return the first run that produces an $i$ and break there.
Suppose we conduct $k$ runs, the event that this repeated version of $ALG$, namely $ALG_k$, fails is if each of the runs return $FAIL$. Thus,
$Pr[ALG_k\ fails] = \frac{1}{3} \cdot \frac{1}{3} \cdots\frac{1}{3} = \frac{1}{3^k}$
i.e., $Pr[ALG_k\ Succeeds] = 1 - \frac{1}{3^k} \geq 1 - \delta$
where the last inequality comes from our requirement on the probability of success and thus leads to the following bound on k:
$\frac{1}{3^k} \leq \delta \implies k \geq \lceil log_3(\frac{1}{\delta})\rceil$
For instance, if we want the Algorithm to find an $i$ (i.e., succeed) with probability at least $0.99999$, then our $\delta = 10^{-5}$ giving a lower bound of $\lceil log_3(10^5) \rceil = 11$. In other words, $11$ independent runs of $ALG$ ensures success with at least probability of $0.99999$
Here's the pseudocode:
(Note: $\delta$ is a parameter that is fixed and independent of $n$)
$FINDINDEX(A,\delta):\\
k \leftarrow \lceil log_3(\frac{1}{\delta})\rceil \\
N \leftarrow A.length \\
\textbf{for}\ j=1\ \textbf{to}\ k\ \textbf{do}\\
\ \ \ \ i \leftarrow RANDOM(N) \\
\ \ \ \ \textbf{if}\ A[i]=X\ \textbf{then}\\
\ \ \ \ \ \ \ \ \textbf{return}\ i\\
\textbf{return}\ NULL $
In the above, $RANDOM(N)$ is assumed to be a function that returns an integer picked uniformly at random from $1$ to $N$
return random(0, 3n)
. Probability = 2/3. What are the constraints on the probability? If it is required to be correct, then constant time might not be possible. Clarification on this probability would be helpful. Also, look into Las Vegas algorithms and Monte Carlo algorithms. Specifically this section: en.wikipedia.org/wiki/… $\endgroup$