Skip to content

Instantly share code, notes, and snippets.

@Navid-JL
Last active February 4, 2024 09:52
Show Gist options
  • Select an option

  • Save Navid-JL/f5e91ca3dfc6e23ee0bf5045467d611c to your computer and use it in GitHub Desktop.

Select an option

Save Navid-JL/f5e91ca3dfc6e23ee0bf5045467d611c to your computer and use it in GitHub Desktop.
True Randomness
# Python 3.6 and later have the secrets module, which uses the operating system’s source of truly random numbers (often gathered from random events, such as the time between the user’s keystrokes). The function secrets.randbelow() can return truly random numbers between 0 and up to but not including the argument passed to it.
# The functions in secrets are slower than the functions in random, so the functions in random are preferred when true randomness is not needed. You can also use the secrets.choice() function, which returns a randomly chosen value from the string or list passed to it.
import secrets
def main() -> None:
secrets.randbelow(10)
secrets.choice(['cat', 'dog', 'mouse'])
secrets.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
if __name__ = "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment