Learn how to use the Pandas module in Python for data analysis. Explore functions, examples, and best practices for efficient data manipulation.
The random
module in Python is used to generate pseudo-random numbers for various probability distributions. It’s commonly used in games, simulations, testing, security, and any application that requires randomness.
random.random()
Returns a random float number between 0.0 and 1.0 (excluding 1.0).
import random
print(random.random())
# Example output: 0.5488135039273248
random.randint(a, b)
Returns a random integer between a and b (inclusive).
print(random.randint(1, 10))
# Example output: 7
random.choice(sequence)
Returns a randomly selected element from a non-empty sequence.
colors = ['red', 'green', 'blue']
print(random.choice(colors))
# Example output: 'green'
random.shuffle(sequence)
Shuffles the elements of a sequence in place.
cards = ['Ace', 'King', 'Queen', 'Jack']
random.shuffle(cards)
print(cards)
# Example output: ['Queen', 'Ace', 'Jack', 'King']
random.uniform(a, b)
Returns a random float number between a and b.
print(random.uniform(1.5, 3.5))
# Example output: 2.783439510426392
random.sample(population, k)
Returns a k-length list of unique elements chosen from the population.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(random.sample(numbers, 3))
# Example output: [5, 9, 2]
random.randrange(start, stop, step)
Returns a randomly selected element from range(start, stop, step).
print(random.randrange(0, 100, 5))
# Example output: 65 (which is in 0, 5, 10, ..., 95)
import random
def roll_dice():
return random.randint(1, 6)
print("You rolled:", roll_dice())
import random
import string
def generate_password(length=8):
characters = string.ascii_letters + string.digits + string.punctuation
return ''.join(random.choice(characters) for _ in range(length))
print("Your new password:", generate_password(12))
import random
def lottery_numbers(pool_size=49, numbers_to_pick=6):
return random.sample(range(1, pool_size+1), numbers_to_pick)
print("Lottery numbers:", lottery_numbers())
import random
players = ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank']
random.shuffle(players)
team1 = players[:len(players)//2]
team2 = players[len(players)//2:]
print("Team 1:", team1)
print("Team 2:", team2)
import random
questions = [
"What is the capital of France?",
"Who painted the Mona Lisa?",
"What is the largest planet in our solar system?",
"In what year did World War II end?"
]
current_question = random.choice(questions)
print("Your question:", current_question)
import random
def generate_discount():
return f"{random.randint(5, 50)}% off"
print("Today's special offer:", generate_discount())
Remember that the random
module generates pseudo-random numbers, which are sufficient for most applications but shouldn’t be used for cryptographic purposes (for that, use the secrets
module in Python).
Watch this video for the answer:
Answer key (Mutiple Choice):
Answer Key (Fill in the Blanks):