random.py 1.0 KB

12345678910111213141516171819202122232425262728
  1. # The secrets module is used for generating cryptographically strong random
  2. # numbers suitable for managing data such as passwords, account authentication,
  3. # security tokens, and related secrets.
  4. # In particularly, secrets should be used in preference to the default
  5. # pseudo-random number generator in the random module, which is designed for
  6. # modeling and simulation, not security or cryptography.
  7. #
  8. # Requires Python 3.6+
  9. # import secrets
  10. import random
  11. import string
  12. def ascii_string (
  13. length = 16,
  14. alphabet = string.ascii_letters + string.digits + string.punctuation):
  15. # return ''.join (secrets.choice (alphabet) for i in range (length))
  16. return ''.join (random.choice (alphabet) for i in range (length))
  17. def alphanumeric_string (length = 16):
  18. return ascii_string (length, string.ascii_letters + string.digits)
  19. def digit_string (length = 16):
  20. return ascii_string (length, alphabet = string.digits)
  21. def hex_string (length = 16):
  22. return ascii_string (length, alphabet = string.hexdigits)