Last active
December 1, 2017 15:35
-
-
Save rcanepa/f23b83825fd89af7ae5eac11089188dc to your computer and use it in GitHub Desktop.
Random String Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import string | |
| import random | |
| # The random module also provides the SystemRandom class which uses the system function | |
| # os.urandom() to generate random numbers from sources provided by the operating system. | |
| os_random = random.SystemRandom() | |
| default_characters = string.ascii_letters + string.digits # 62 characters | |
| def random_string(length=12, characters=default_characters): | |
| """Return a random string by randomly picking `length` characters | |
| from `characters`. The number of options is `len(characters) ** length`. | |
| For `length = 10` and `characters=default_characters` (62 characters), | |
| there are 839,299,365,868,340,224 options.""" | |
| return "".join([os_random.choice(characters) for _ in range(length)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment