For a package I am writing, I need unique numbers between 0 and 2**33 in a random order. Initially, I tried to use a Generator as follows:
def randomnumber(NUM):
List = [i for i in range(NUM)]
List.shuffle()
index = 0
while index < NUM:
index += 1
yield List[index-1]
But as NUM is 2**33 in my case, this code is just impossible. I have tried to write all the numbers to a text file from bash and found out the file is of size 93.6 GB (which is really huge and far more than my RAM). Then I am shuffling the contents of the file using terashuf and reading every line through it using linecache
.
Also, I am using the multiprocessing module (apply_async in particular) and really need to pass this generator object as an argument. But python gave an error stating it can't use a generator object in pool processes. I went through few questions on SO and one it's answer is to create a list of these numbers from the generator for few numbers and pass them as arguments to the function running parallely, but that didn't work either.
So my question is there any way in which we can make create a generator which does the intended work (of giving random unique number between 0 and 2**33) or some other way to do this as I don't want to shuffle the contents of the file again and again (takes quite a lot of time)