jeudi 15 octobre 2020

Lazy random number generation without replacement in R

I want to generate random values from vector 1:n without replacement, just as sample(n) would do. However, instead of saving the permutation in memory, I want to generate the values on demand. similar to a generator in Python.

I imagine something like this:

# not working
rng <- random_permutation(n)  # 'on demand' random number generator
x <- next(rng)  # lazy creation of new random value (w/o replacement)

Why do I need this? Because n can be very large, and often only few random values will be needed. Storing the entire 1:n vector in memory would be very inefficient and not very elegant.




Aucun commentaire:

Enregistrer un commentaire