I have a function here that returns a 4 digit string. The problem is that when I run the function like 500 times or more, it starts to return duplicates. How to avoid that?
My Function:
import random
def CreatePass():Num = str(random.randint(1000, 9999)return Num
Generate a list, shuffle that and pop from it each time the function is called:
import randomdef CreatePass(_numbers=[]):if not _numbers:_numbers[:] = range(1000, 10000)random.shuffle(_numbers) return str(_numbers.pop())
Note that this re-generates the _numbers
list once you've run out, but then you've used up all 8999 possible numbers and would have to accept repetitions anyway.