You Might Have Misused Python Random

Image for post
Image for post
Photo by Lucas Santos on Unsplash

Introduction

Basic usage of Python random module

#Generate a random integer between 1 to 10 
random.randint(1,10)
#2
#generate a random floating point number between 0 to 1 random.random()
#0.3103975786510934
#Generate random number between 1 to 2 in uniform distribution
random.uniform(1, 2)
#1.9530600469459607
#Generate random number between 1 to 100, with step as 2
random.randrange(1, 100, 2)
#43
#Generate random bytes, available in Python version 3.9 random.randbytes(8)
#b'v\xf7\xb2v`\xc8U]'
#Generate a integer within 8 bits
random.getrandbits(8)
#68
slangs = ["Boomer", "Cap", "Basic", "Retweet", "Fit", "Fr", "Clout"]random.shuffle(slangs) 
#['Fit', 'Basic', 'Fr', 'Clout', 'Cap', 'Retweet', 'Boomer']

random.sample(slangs, k=5)
#['Fit', 'Fr', 'Clout', 'Retweet', 'Basic']
random.choice(["Boomer", "Cap", "Basic", "Retweet", "Fit", "Fr", "Clout"])
#'Retweet'

Why the random numbers generated are not random enough?

sys_random = random.SystemRandom()sys_random.random() 
#0.04883551877802528
sys_random.randint(1, 100)
#72
sys_random.choice(["Boomer", "Cap", "Basic", "Retweet", "Fit", "Fr", "Clout"])
#'Clout'

Python secrets module

#Generate random integer between 0 to 50 
secrets.randbelow(50)
#2
#Generate random integer with 8 bits
secrets.randbits(8)
#125
#Generate random bytes
secrets.token_bytes(20)
#b'\x8a\xb3\xa92)S:\xf2\xac\x90\xaf\xb1\xb3Q\xc5\xfe\x80\xdb\xc2`'
#Generate random string in hexadecimal with default 32 bytes
secrets.token_hex()
#'cf4f964edf810ca7f6ad6b533038fdcf538c73bd59e11d3340a838e7fd88fdf9'
#Generate URL-safe text string
secrets.token_urlsafe(10)
#z9zQQsxcq6SRug
import string secrets.choice(string.ascii_letters + string.digits + string.punctuation)
#'k'

Implementing a strong password generator with Python secrets

def generate_strong_password():    special_characters = '!#$%&@_~' 
password_choices = string.ascii_letters + string.digits + special_characters
while True:
password = ''.join(secrets.choice(password_choices) for _ in range(random.randint(8, 16)))
if (any(c.islower() for c in password)
and any(c.isupper() for c in password)
and sum(special_characters.find(c) > -1 for c in password) == 1
and any(c.isdigit() for c in password)):
break

return password
[generate_strong_password() for _ in range(10)] # sample output 
[
'C9A&PbswcLMpJ',
'DHrbuzZU&io7Io',
'B1zx4YqKUS@01m',
'Q!F8tsZJsw',
'9QVF8oASt_jceq2',
'2~EKErrHAc9jvbyZ',
'8ceU_XZbYdqv8b',
'h8oS@tZ6',
'3w@OX33tw12J6',
'FX~mkO0aNatqp'
]

Conclusion

Written by

Resources and tutorials for python, data science and automation solutions

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store