Course Content

  • Random module

Course Content

Python Random Module

Hello guys. Welcome to LearnVern. In the last topic we saw Function.

Today we will work with random modules.

What is a Random Module?

Random Module is one type of Inbuilt function which helps in choosing or generating random numbers.

It is compulsory to import it during coding in Python.

I’ll show you how, in practicals.

Here are some Random numbers operations.

Operations meaning you can do certain work through a particular functionality.

Such as our Choice method. What does the Choice method do?

The choice inbuilt function is used to pick up a random number from your list, set or collection.

There is a Randint function in the same way. It is used to generate numbers in a range.

For eg: our OTPs are generated using this operation only.

3rd is Shuffle. It is used to shuffle a value of a list.

It is also a part of a random module.

What are we going to do today in practicals?

We will see how to import random modules & how to execute random operations.

Let’s move onto our practical part where I’ll explain everything in detail about random modules.

The Jupyter is open only.

First of all, we have to import.

I’ll write here Import Random.

How to import? It’s easy.

From random, that is our module’s name & import.

You can use any function you want after Import.

Otherwise what is the other way? I’ll write functionname.

And make it into a comment as I don’t have to use it right now.

Other than that I can use Import random.

You can import random in these 2 ways.

How to use them?

Our 1st function is the choice function.

Let’s see how to use it.

Choice function randomly generates value from your collection.

I’ll make a list here L1. In which I passed the values. Total 6 values.

It will randomly choose for us. We will use our Choice function.

And write L1 here.

But if you are using the Choice function here, you’ll have to import it here on the top first of all.

How to import?

Write Random module import choice. So that our choice here can use it.

It automatically picked up a number from the list. It was a random number.

Moving forward to our 2nd function which is Randint.

This function is used to generate random numbers within a specific range.

Randint itself is a part of Random module.

We will again have to import the Random module & write Randint here.

The Randint function is imported. Now how to use it?

I took a variable named OTP. In that, we’ve set a range named Randint.

I want to generate my OTP within this range.

Now I’ll print my OTP.

I’ll write a message here: Your OTP. Print it.

You can see that our OTP has been generated.

Moving to the next topic in the Random module that is Shuffle.

So how does Shuffle work?

I know most of you must have played Candy Crush or known about it.

The candies used to shuffle in the game, matched the candies, removed them & again it used to shuffle and come on the screen.

Shuffle method works in the same way here.

We will import Random module once again.

Shuffle it. Now we will make a list. L2.

And take some strings here: Apple, Banana, Mango.

I took 3 strings here. And then shuffle it.

But we have to store it somewhere after shuffling it.

The shuffle value cannot print without storing.

I’ll take X here and use the Shuffle & pass L2 parameter.

And then print X. Why didn’t our values get printed here?

Because we don’t have to save it in X, we only have to shuffle it & write L2.

We only have to shuffle the list. Not store it anywhere.

Shuffle & print the list.

The more I run it, the more it will shuffle.

Shuffle will automatically shuffle your list.

This is how you can use random modules after importing.

So what did we learn today?

We learned how to import random modules & we worked with the operations of the random modules.

This is how a random module works.

In the next video we will learn about the Math module.

Okay everyone? Thank you.

FAQs

The Python Random module is a built-in module for generating random numbers in Python. These are pseudo-random numbers, which do not represent true randomness. This module can be used to do things like generate random numbers, output a random value for a list or string, and so on.

To get access to the random module, we add from random import * to the top of our program (or type it into the python shell). Open the file randOps.py in vim, and run the program in a separate terminal. Note if you run the program again, you get different (random) results.

Python modules are packages of code that help to perform specific actions within a program. There are many different modules in Python, but the most popular one is the Random module. Using this module, users can create random numbers or strings.

The Python Random module is a great tool for generating random numbers, sequences, and other random data. The module includes functions for generating random integers, floating point values, strings of characters, and words.

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere