5 Simple but Fun Projects with Random Module

The Random Module is one of the most commonly used modules in Python. We can use it in anything, from a dice roll to a Chatbot program. However, let's start small. Here are 5 quick programs to get you associated with the Random Module. 

#1. Coin Toss: 

The most basic random outcome is a coin toss. We have just two possibilities - Heads and Tails. So, converting that into a Pythonic code is not hard at all. Here it is: 

Random Python Function





Two lines of code... That's all you need to get going with #1. The random.choice() function picks a random element from a list. Here, our list contains the strings - 'Head' and 'Tail'. So, one of them will be randomly picked, just like a real coin toss. 

#2. Dice Roll:

Another classic example of a random occurrence is a dice roll. Here, the result must be a random integer number between 1 and 6. So, we can use another function random.randint(a,b), which picks a random integer between a and b (a and b included). In code, it would look like this: 

random python module




#3. Draw a Playing Card: 

A deck of playing cards has 52 cards and they are divided into 2 colors, 4 suits and 13 cards in each suit. Most games involve a random draw of a card from the deck and therefore, it is another very good project to use the random module. 

random python code







#4. Random Password Generator:

Many people like to use random password generators to create passwords that are hard to guess. They too use random generation mechanisms to generate a password with multiple random characters/numbers in random positions. For starters, let's create a password with 3 numbers and 8 characters. 

random python example







#5. Magic 8 Ball:

A Magic 8 Ball is a toy that gives the user a random answer to any question they think of. It uses a 20-sided piece with answers written on each side. We can do the same in Python but without any 20-sided piece. How ? The Random Module, of course. We can start with 5 responses and go all the way to hundreds of responses, assuming you have the patience.

random generator










Now, the outputs. 

#1. 



#2. 





#3.






#4.




#5. 




<Note> Your outputs may not be the exact same ones here. Don't fret, however. The random module gives everyone different outputs for different tries. </Note>

So, here is 5 small but quick and fun projects you can try with the Random Module. 

Feel free to comment down below if you have any questions or suggestions.

Make sure to follow the blog to be updated as soon as a post is released. Just click the blue 'follow' button in the menu to follow.

If you want to familiarize yourself a little more with Random, check out the projects in this page: The PYgrammer: PYTHON GAMES COMPILATION

If you want to check out other fun modules, have a look at this: MATPLOTLIB - GRAPHS WITH PYTHON (thepygrammer.blogspot.com)


Comments