Interested in Personalized Training with Job Assistance? Know More
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.
Have a friend to whom you would want to share this course?