Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Is there any module in Python for generating random strings but to be unique ? I need to generate keys like for example when installing Windows.
os.urandom(size)
ssl.RAND_bytes(size)
Since you haven't specified the format of the string you want to get, I suppose it doesn't matter, so I suggest simply using UUIDs.
>>> import uuid >>> str(uuid.uuid4()) > '3afc84bb-6d73-4482-806a-6b3a29e43bca'
Add a comment
Well if you want only letters, for example, here's code to generate a random string of a random length upto 1000:
out = '' for i in range(random.random()*100): out += random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvqxyz')
You can modify your alphabet of course.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
os.urandom(size).ssl.RAND_bytes(size)(Python 3.3) provides cryptographically strong pseudo-random bytes.