Suppose I have a $p$-dimensional random vector $X$, with probability mass function $f(x) = P(X = x)$, $x \in \mathbb \{0,1\}^p$. Each of the $p$ variables is binary. If I want to sample from the distribution described by $f$, I can calculate the probability of each of the possible $2^p$ outcomes, divide the the $[0,1]$ interval in $2^p$ sub-intervals so that the $i$-th sub-interval has length equal to $P(X = i\text{-th possible outcome})$, sample a pseudonumber $u$ from a uniform distribution on $[0,1]$ and then select $X = i\text{-th possible outcome}$ if $u \in $ the $i$-th sub-interval.
The problem is that this method requires a $2^p$-dimensional vector containing the probabilities of all outcomes, and it does not fit in memory if $p$ is too big (and for my problem I need something like $p$ > 150).
Is there a more efficient way to do this sampling?
Thank you very much