Let's say I want to generate a shuffled list of 1000 unique numbers (the numbers can be just a range from 1 to 1000).
I can do that by expanding a range from 1 to 1000 to an actual list and then randomly shuffling elements. Language doesn't matter, but in python it might be implemented like this: random.shuffle(list(range(1000)))
.
But what if I need to generate, let's say, 10 billion numbers? Expanding the range to a list would require a lot of memory (about 74 GB of memory if a number is stored in 8 bytes). Shuffling would also require a lot of memory and time. But it's not required to generate all the numbers at a time, but instead it would be better to generate them one by one, saving the state, and not filling up the memory by storing all the numbers, but only one instead.
It's still required for numbers to be unique. Is there any algorithms for this purpose?
It would be also great if there was a way to fast restore the "state" of the generator at a given generation step. For example, if I have already generated 1 million numbers and the next number must be 10, it would be great if I could efficiently (without regenerating that million numbers again) restore the state at a step of 1 million and generate the next number - 10.
Are there such algorithms?