Python

Python Random Module Tutorial

Hello in this tutorial, we will understand how to use the random module in python programming.

1. Introduction

The random module in python programming is used to generate random numbers for various purposes. The random number generated can be an integer or float. This module provides a different set of methods and in this tutorial, we will study and implement some of them.

MethodDescription
randint(minimum, maximum)Generates a random integer between the given min and max integers
randrange(beg, end, step)Returns a randomly selected element from the range created by the beginning, ending, and step arguments. The default value is beginning is 0 and step is 1
choice(sequence)Generates a randomly selected element from a non-empty sequence. If the sequence is empty IndexError is thrown by the python compiler
seed(seed_value)Initialize the random number generator with a seed value. Returns the mapper
shuffle(number_list)Randomly reorders the elements in a list
sample(type, x)Returns an X sized random samples from a type such as a list or set
uniform(beg, end)Returns a random floating-point number within a range

1.1 What Is Cryptographically Secure?

The random numbers generated by the random module in python programming is not cryptographically secure. The securely generated random numbers use the synchronization methods to ensure that no two processes can obtain the same data simultaneously. In python 3.x you can use the secrets module to generate secure random numbers.

1.2 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. Python Random Module Tutorial

I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of your choice. Let us dive in with the programming stuff now.

2.1 Python Random Module Tutorial

Let us understand the different use cases of the random module in python programming.

random module

# python random tutorial
import random
# generate random integers
# randint() method generates a random integer between the given min and max integers
def random_integer(minimum, maximum):
    rand_number = random.randint(minimum, maximum)
    print("Random integer number is = {}".format(rand_number))
# generate random floats
# random() method generates a random floating number between 0.0 to 1.0
def random_float():
    rand_number = random.random()
    print("Random float number is = {}".format(rand_number))
# generate random number within range
# randrange() method generates a randomly selected element from the range created by the
# beginning, ending, and step arguments
# default value is beginning is 0 and step is 1
def random_range(beg, end, step):
    rand_number = random.randrange(beg, end, step)
    print("Random number from range is = {}".format(rand_number))
# select random elements
# choice() method generates a randomly selected element from a non-empty sequence
# if sequence is empty -> IndexError is thrown by the python compiler
def random_choice(sequence):
    rand_number = random.choice(sequence)
    print("Random number from sequence is = {}".format(rand_number))
# shuffle elements
# shuffle() method randomly reorders the elements in a list
def random_shuffle(number_list):
    random.shuffle(number_list)
    print("Randomly reordered = {}".format(number_list))
# driver code
if __name__ == "__main__":
    print("==== Python Random Tutorial ====")
    print("\n")
    random_integer(10, 50)
    print("\n")
    random_float()
    print("\n")
    random_range(5, 10, 2)
    print("\n")
    random_choice([5, 10, 15, 20, 25])
    print("\n")
    random_choice('python')
    print("\n")
    random_shuffle([15, 20, 25, 30, 35, 50, 100])

If everything goes well the following output will be shown in the IDE console.

Console Output

==== Python Random Tutorial ====
Random integer number is = 47
Random float number is = 0.0019020537095424395
Random number from range is = 5
Random number from sequence is = 5
Random number from sequence is = o
Randomly reordered = [30, 100, 35, 25, 20, 15, 50]

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned:

  • random module in python programming
  • Sample program to understand the random module use cases

You can download the source code of this tutorial from the Downloads section.

5. Download the Project

This was a tutorial on random module in python programming.

Download
You can download the full source code of this example here: Python Random Module Tutorial

Last updated on Feb. 24th, 2022

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button