You Might Have Misused Python Random

codeforests
4 min readDec 27, 2020
Photo by Lucas Santos on Unsplash

Introduction

Python random module provides a convenient way for generating pseudo-random numbers in case you need some unpredictable results for your application such as the computer games, a lucky draw system or even the shuffling logic for your music player. Since it provides various functions to generate results in “unpredictable” manner, developers attempted to use this feature to produce random password or authentication token for security purpose without understanding of it’s fundamental implementation. In this article, we will be discussing how the Python random module has been misunderstood and misused for the scenarios which it shall not be used.

Basic usage of Python random module

Let’s take a look at some basic usage of this module. You can use it to generate random integers, float numbers or bytes as per below:

#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

--

--

codeforests

Resources and tutorials for python, data science and automation solutions