How to create a sample of a desired size with rejection sampling from a custom distribution? I have implemented rejection sampling on a customized distribution in R. The code appears to work, however due to the nature of rejection sampling, there is a certain proportion of values that get rejected so I was wondering how I could create a sample of a desired size?
By this I mean the following. Suppose I desire a sample of size $n$. When I perform the rejection sampling procedure on $n$ values, I end up with a sample of size $m$ where $m < n$ from my custom distribution. I could try running the procedure $N$ times where $N > n$, but then I would end up with a sample of size $M$ where $M > n$ (for a choice of sufficiently large $N$). If I end up with $m$ points, I have fewer points than I need. If I end up with $M$ points, then I have more points than I need and I don't know which points to eliminate.
Can someone advise me on how to solve this please?
Thank you for looking at my post.


I have more points than I need and I don't know which points to eliminate.

It doesn't matter! They are all i.i.d., so any unbiased rule (not based on looking at the values themselves) is fine -- e.g., throw away the ones generated last, or first. You could even generate random indexes to throw away but there's no need to bother. All of these give the same statistical properties for the resulting size-$n$ sample.
whuber's answer notes this -- and also does a lot of other analysis to estimate how large an initial sample is needed to generate $n$ points -- however, I don't think that's the core problem you had. You can always generate more points if you don't get enough in the first sample. It seems the only thing you were really "missing" is that you can freely throw away points when you have too many.
