2

I'm trying to create (or find) an algorithm to iterate over a range (e.g.: 1-100, etc) but randomly, without any duplicate values (similar result to random sort, etc) but without actually storing the full range in memory (such as using list(range(1, 100)) in Python, etc).

Problem is, I'm not sure how to tackle this. As mentioned, I know about random sort as mentioned, but I would need to store the full range in memory (or large part of the range). Besides that, there are many other way, but they all depend on the full range being stored in memory too.

Now how random it should be? I don't really mind the quality of the randomness. For comparison’s sake, I want it to be as random as using randrange from the random module in Python. As long as it "look" random and does not have any duplicate.

Is there any way to do this?

1 Answers1

4

One approach is to use the sequence of values $E(1), E(2), E(3), E(4), \dots$ where $E$ is a format-preserving encryption cipher with a random key. This ensures the sequence won't repeat and that each element in the sequence will be in the desired range.

See also Pseudo random, unqiue integer numbers in a given range, Shuffling a collection too large for memory.

D.W.
  • 167,959
  • 22
  • 232
  • 500