Writing some fun programs using 'Random' Module in Python - BoiCoder

BoiCoder

Coding Made Easy for All

Breaking

Friday, 2 October 2020

Writing some fun programs using 'Random' Module in Python



Hey all, this is the first article belonging to our 'Fun Friday' series where we write some fun programs. In this article, we are going to use the awesome 'Random' module to make some really interesting programs. I hope you will enjoy.

Firstly we are going to discuss the syntax and the working of the Random module. So get ready with your code editor as it is going to be really fun!

What is Random module and what does it do?

Suppose you wanted to make a program which would automatically chooses a random number from a given list of numbers and present it to you. Rather than writing such a hefty program, Python has given you an easy option to use an inbuilt module called the 'Random' Module.

To add the 'Random' module in your program, you can just write:

import random

This module has many 'methods' that you can play with. You can find a full list of all the methods supported by Random module here:

So as you now know the perquisites of using the Random module, we can start with coding some genius programs. Let's start!

#1: Making a program that rolls a dice for you: 

In this program we are going to use the Random module to randomly choose a number between 1 to 6 and then show it to the user. Of-course, we are not using GUI.

Here's the code:

P.S. We have written range to be from 1,7 and not 1,6 because we wanted 6 to be included in the list, if we would have chosen the latter one, 6 would have not been included.

#2: Making a program that asks the user for a list of friends and then randomly picks the best friend for them

In this program, we are going to ask the user for a list of friends and then randomly display them their best friend. Interesting? Yes it is! Let's start coding!!!

Here's the code:

You can see that in this program, first we used the split function to turn user input (string) into a list. Then we have used the random.choice method to choose one random friend as your best friend.

#3:Making a program to irritate your friends by automatically opening random websites:


In this program, we are going to open random websites automatically so that you can screw up your friends! Excited? Let's code:

Here's the code


Here, we have used random, time and webbrowser modules in python to write a program that would automatically open websites after a fixed time.

You can learn about the Webbrowser Module here: Webbrowser Module Python- GeeksForGeeks

You can learn about the Time Module here: Time Module Python- Programiz

Don't forget to make an .exe file of it, so that you can send it to your friend!

Note: To stop this program, you can use Ctrl + C.


#4 Making a random password generator with Random Module in Pyhton:


In this program, we are going to make a random password for the user. Let's see:

Here's the code:

As you can clearly see, the program is very simple. At first we have declared a variable called 'character' which contains all letters, numbers and special characters that can be used to make a password. Next we asked the user for number of passwords to be generated and stored it in the variable 'number'. 

Next we would ask the user for the length of password. After we have got all that, we would use a simple nested for loop along with random.choices method to make a random password. See how easy was this!

------------------------------------

So that's it for this tutorial. I hope you learnt something new. This is not the end of our journey, the Random Module in Python has immense possibilities of making fun but useful programs. I have tried to make these programs interesting but also short and easy to understand.


I really hope you liked this program, and if you did then don't forget to share it as this may help someone in need! And please subscribe to our newsletter as we do these amazing coding sessions regularly.

Again, Thanks for Reading!!!

2 comments:

  1. Oye koi python module hogi with list of website warna webcrawller se ek badi list bna le thodi websites me mza nhi ayega

    ReplyDelete
    Replies
    1. I wrote few just for good readability. But ofcourse, you can add n number of sites to it.

      Delete