Exploring the Magic of Python's Random Module

Alvison HunterAlvison Hunter
3 min read

Howdy, dear readers! Have you ever wondered how to create a deck of cards and shuffle them or how to generate random numbers for a game? Well, if you had, and you would like to learn about it, you’re in the right place!

In this article, we’ll explore five incredibly useful functions from Python’s random module that will make generating random results a easy-peasy lemon-squeezy game. Whether you're a seasoned coder or a complete beginner, you'll find these examples easy to understand and fun to implement. So, let's get into the fascinating world of randomness in Python!

1. Shuffle
Imagine you have a deck of cards and you want to shuffle them. The shuffle function is perfect for this task! Let’s see how we do that, ok?

import random

# A list representing a deck of cards
deck = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']

# Shuffling the deck
random.shuffle(deck)

print("Shuffled deck:", deck)

This function takes a list and shuffles its elements in place. Every time you run the code, you’ll get a different order of cards!

2. Randint
Need a random integer? The randint function is your friend. It generates a random integer between two specified values, inclusive. Let’s take a look:

import random

# Generating a random integer between 1 and 10
random_number = random.randint(1, 10)

print("Random integer between 1 and 10:", random_number)

3. Choice
The choice function is great for picking a random item from a list. Let’s choose the best football player from a given list, shall we?

import random

# A list of players
best_player = ['Cristiano Ronaldo', 'Leonel Messi', 'Kylian MBappe', 'Jude Bellingham']

# Picking a random player from that list
random_player = random.choice(best_player)

print("Best Player:", random_player)

Every time you run this, you’ll get a different player from the list. Please bear in mind that the best player on that list to ever exist in this world is undoubtedly Mister Cristiano Ronaldo, CR7, also known as “El Comandante, siiiuuuuu!!!”.

4. Sample
What if you need multiple unique random items from a list? sample has got you covered. This function ensures that the same item is not picked more than once.

import random

# A list of some of the best nica fruits
nica_fruits = ['mango', 'banano', 'jocote', 'naranja', 'zapote', 'sandia','melon', 'papaya']

# Picking 3 random fruits from that list
random_fruits = random.sample(nica_fruits, 3)
print("3 Popular nica fruits:", random_fruits)

5. Seed
Reproducibility is key in some applications. The seed function allows you to generate the same random numbers each time you run the code.

import random
# Setting the seed
random.seed(42)

# Generating random numbers
print("Random number 1:", random.randint(1, 100))
print("Random number 2:", random.randint(1, 100))

Setting a seed ensures that the sequence of random numbers is the same every time you run your code.

Conclusion
And there you have it! We’ve covered five powerful functions from Python’s random module: shuffle, randint, choice, sample, and seed. These functions are incredibly versatile and can be used in a variety of applications, from games to simulations. So go ahead, try them out, and add a bit of randomness to your Python projects! Happy coding, folks!

❤️ Enjoyed the article? Your feedback fuels more content.
💬 Share your thoughts in a comment.
🔖 No time to read now? Well, Bookmark for later.
🔗 If it helped, pass it on, dude!

0
Subscribe to my newsletter

Read articles from Alvison Hunter directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Alvison Hunter
Alvison Hunter

JavaScript & Python Dev | Managing Partner @CodeCraftersLabs. Linguist & Music Enthusiast | Husband | Father of 2 brave warriors.