DEV Community

Cover image for Exploring the Magic of Python's Random Module
Alvison Hunter Arnuero | Front-End Web Developer
Alvison Hunter Arnuero | Front-End Web Developer

Posted on • Updated on

Exploring the Magic of Python's Random Module

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (0)