Skip to content

Instantly share code, notes, and snippets.

@rcanepa
Last active December 1, 2017 15:35
Show Gist options
  • Select an option

  • Save rcanepa/f23b83825fd89af7ae5eac11089188dc to your computer and use it in GitHub Desktop.

Select an option

Save rcanepa/f23b83825fd89af7ae5eac11089188dc to your computer and use it in GitHub Desktop.
Random String Generator
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