I am working on a problem where, given the binomial probability density function: $$X(p,n,k) = \left(\frac{n!}{k!(n-k)!}\right)p^k(1-p)^\left(n-k\right)$$ I need a function $Y(p,n,x) = k$ where $p, n$ and $k$ carry the same meanings as the parameters in the binomial distribution, and $x$ is a random number $\in [0,1]$ chosen via an arbitrary random process.
The purpose of $Y$ is to allow for the random generation of integers, $k \in [0,n]$, for a given value of $p$ and $n$, via the random parameter $x$, such that the likelihood of generating k is exactly $X$. I need a closed form for $Y$ or an approximation method that is not stochastic. There are functions in the C++ standard library that claim to provide this functionality but are somehow internally stochastic and inconsistent across hardware/operating system changes are therefore unusable to me.
I've seen a similar question posed in this stack exchange page but it did not provide what I am looking for.
Is such a function $Y$ even possible? In attempting to solve it myself I came across this stack exchange page on calculating the inverse of $n!$ using the Lambert W function, which I will need some time to fully understand before I can comfortable use in my work. An additional issue with the $W$ function is that I would eventually need an algorithmic way to represent $W$ in a computer program.
qbinom()
- but it is not what your question says. – Henry Oct 26 '18 at 21:46rbinom()
to generate binomial random variates. If you fix a seed then you get the same sequence of values each time. – Trevor Gunn Oct 26 '18 at 21:54