Section 1/4Essential Random Functions
Essential Random Functions
random.randint()
Mastering the Random Module
Python's random module offers various ways to generate randomness. Let's explore its most useful functions for different scenarios.
Essential Random Functions
random.randint()
# Random integer in range [a, b]dice = random.randint(1, 6)
random.choice()
# Random element from sequencefruits = ["apple", "banana", "cherry"]snack = random.choice(fruits)
random.shuffle()
# Shuffle list in placecards = list(range(52))random.shuffle(cards)
random.sample()
# Unique random selectionwinners = random.sample(players, 3)