Fix a function returning duplicates over time?

2024/7/7 7:51:27

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
Answer

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.

https://en.xdnf.cn/q/120658.html

Related Q&A

Pandas method corr() use not all features

I have dataframe with shape (335539, 26). So I have 26 features. But when i use data.corr() I get a 12 x 12 matrix.What can be wrong? `

int to datetime in Python [duplicate]

This question already has answers here:Convert string "Jun 1 2005 1:33PM" into datetime(26 answers)Parsing datetime in Python..?(2 answers)Closed 5 years ago.Im receiving data from the port.…

How to extract the historical tweets from twitter API? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

ValueError: invalid literal for int() with base 16: [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Modify list in Python [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

How to delete files inside hidden folder with python? [duplicate]

This question already has answers here:How can I delete a file or folder in Python?(18 answers)Closed 5 years ago.I want do delete a file, for ex. myfile.txt that is stored under a hidden folder. Is i…

Matching keywords in a dictionary to a list in Python

The following dictionary gives the word and its value:keywords = {alone: 1, amazed: 10, amazing: 10, bad: 1, best: 10, better: 7, excellent: 10, excited: 10, excite: 10}Following the dictionary are two…

How to make case insensitive? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Webpage data extraction automation using python and selenium webdriver [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Asking the user if they want to play again [duplicate]

This question already has answers here:Ask the user if they want to repeat the same task again(2 answers)Closed 5 years ago.Basically its a guessing game and I have literally all the code except for th…