Cybersecurity: passwords and safe randomness
A good password is above all long and unpredictable. Attackers first try the most common passwords (azerty, P@ssw0rd…): swapping an a for an @ fools nobody. A serious checker therefore rejects known passwords, then gives points for length and variety of characters.
To generate a secret (password, login token, reset link), you need cryptographic randomness: the secrets module. The random module is made for games and simulations: its draws can be predicted, so it must never be used for security.
import secrets, string
alphabet = string.ascii_letters + string.digits + "!?#@"
password = "".join(secrets.choice(alphabet) for _ in range(16))
print("length:", len(password))
print("token:", len(secrets.token_hex(16)), "hex characters")