Random Modules
What You'll Learn: In this tutorial, you'll discover how to use Python's built-in random module to generate random numbers and perform various random operations. This module includes many useful methods for working with randomness.
Key Functions in the random Module
seed()- Description: Initializes the random number generator. Useful if you want to make random numbers predictable for testing purposes.
Example:
pythonimport random random.seed(42) # Sets the seed for random number generation
getstate()- Description: Returns the current internal state of the random number generator. This can be saved and used later to restore the state.
Example:
pythonimport random state = random.getstate() # Gets the current state
setstate(state)- Description: Restores the internal state of the random number generator to a specific state. This allows you to continue generating random numbers from where you left off.
Example:
pythonimport random random.setstate(state) # Restores the state
getrandbits(k)- Description: Returns a number representing
krandom bits. Example:
pythonimport random bits = random.getrandbits(5) # Generates a random number with 5 bits print(bits)
- Description: Returns a number representing
randrange(start, stop[, step])- Description: Returns a random number between the given range. It can take an optional
stepparameter to specify the increment. Example:
pythonimport random num = random.randrange(1, 10, 2) # Generates a random number between 1 and 10 with step 2 print(num)
- Description: Returns a random number between the given range. It can take an optional
randint(a, b)- Description: Returns a random number between
aandb(inclusive). Example:
pythonimport random num = random.randint(1, 10) # Generates a random number between 1 and 10 print(num)
- Description: Returns a random number between
choice(seq)- Description: Returns a random element from the given sequence.
Example:
pythonimport random fruit = random.choice(['apple', 'banana', 'cherry']) # Chooses a random fruit from the list print(fruit)
choices(population, weights=None, *, cum_weights=None, k=1)- Description: Returns a list with a random selection from the given sequence.
Example:
pythonimport random x = random.choices(['apple', 'banana', 'cherry'], k=2) print(x)
shuffle(x[, random])- Description: Takes a sequence and returns the sequence in a random order.
Example:
pythonimport random my_list = [1, 2, 3, 4, 5] random.shuffle(my_list) print(my_list)
sample(population, k)- Description: Returns a given sample of a sequence.
Example:
pythonimport random x = random.sample(['apple', 'banana', 'cherry'], k=2) print(x)
random()- Description: Returns a random float number between 0 and 1.
Example:
pythonimport random x = random.random() print(x)
uniform(a, b)- Description: Returns a random float number between two given parameters.
Example:
pythonimport random x = random.uniform(1, 10) print(x)
triangular(low, high, mode)- Description: Returns a random float number between
lowandhigh, with a specifiedmodeparameter for the midpoint.
- Description: Returns a random float number between
Try It Yourself: Fun Exercises
- Generate Random Numbers:
- Use
randint()to generate random numbers for a simple dice-rolling game.
- Use
- Randomly Select Items:
- Use
choice()andsample()to randomly select items from a list.
- Use
- Shuffle a List:
- Use
shuffle()to randomize the order of elements in a list.
- Use
Summary:
In this Python tutorial, we learned how to use some basic functions from the random module to generate random numbers and make random choices. These functions are handy for adding randomness to your programs. Keep experimenting and have fun with Python!