The random
module in Python is a built-in module that provides a suite of functions for generating random numbers and sequences.
Here are some of the commonly used functions in the random
module:
random()
: Returns a random float between 0 and 1.randint(a, b)
: Returns a random integer betweena
andb
(inclusive).randrange(start, stop[, step])
: Returns a randomly selected element from the range created by the arguments.choice(seq)
: Returns a random element from the given sequence.shuffle(seq)
: Shuffles the elements of the given sequence randomly.
Here’s an example of using the random
module to generate a random integer between 1 and 100:
import random random_number = random.randint(1, 100) print(random_number)
Output:
42
Generate Random Numbers within a Defined Range:
To generate random numbers within a defined range in Python, you can use the randint()
function from the random
module.
Here’s an example:
import random # Generate a random integer between 10 and 20 random_number = random.randint(10, 20) print(random_number)
Output:
15
In this example, randint(10, 20)
generates a random integer between 10 and 20 (inclusive). You can adjust the arguments to set your desired range.
If you need to generate a sequence of random numbers within a range, you can use a loop:
import random # Generate 10 random integers between 1 and 100 for i in range(10): random_number = random.randint(1, 100) print(random_number)
Output:
51 28 85 79 13 24 17 73 58 44
In this example, the loop generates 10 random integers between 1 and 100.
Select Random Elements:
To select random elements from a sequence in Python, you can use the choice()
function from the random
module.
Here’s an example:
import random fruits = ['apple', 'banana', 'orange', 'kiwi', 'mango'] # Select a random fruit from the list random_fruit = random.choice(fruits) print(random_fruit)
Output:
orange
In this example, choice(fruits)
selects a random element from the fruits
list. You can replace fruits
with any other sequence, such as a tuple or a string.
If you need to select multiple random elements from a sequence, you can use the sample()
function:
import random numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Select 3 random numbers from the list random_numbers = random.sample(numbers, 3) print(random_numbers)
Output:
[5, 2, 7]
In this example, sample(numbers, 3)
selects 3 random elements from the numbers
list without replacement. If you want to allow duplicates, you can use the choices()
function instead.
Random Seed:
In Python, you can use the random.seed()
function to initialize the random number generator with a specific seed value. The seed value is used to generate a sequence of random numbers, and if the same seed value is used again, the same sequence of random numbers will be generated.
Here’s an example:
import random # Initialize the random number generator with seed value 42 random.seed(42) # Generate 5 random integers for i in range(5): print(random.randint(1, 10))
Output:
4 10 7 3 7
In this example, random.seed(42)
initializes the random number generator with seed value 42. Then, randint(1, 10)
generates 5 random integers between 1 and 10 (inclusive), but because the random number generator was initialized with the same seed value, the same sequence of random numbers is generated each time the program is run.
Using a fixed seed value can be useful when you need to generate a random sequence of numbers that can be replicated, such as for testing or debugging purposes. However, if you need true randomness, you should not use a fixed seed value. In that case, you can use random.seed()
with no arguments, which uses the system time as the seed value.
Various Functions of Random Module:
The random
module in Python provides various functions to generate random numbers, shuffle sequences, and select random elements. Here are some of the most commonly used functions:
random()
: Generates a random float between 0 and 1 (exclusive).randint(a, b)
: Generates a random integer between a and b (inclusive).randrange(start, stop[, step])
: Generates a random integer from the range specified bystart
,stop
, andstep
.uniform(a, b)
: Generates a random float between a and b (inclusive).choice(seq)
: Selects a random element from a sequence.sample(population, k)
: Selects k random elements from a population sequence without replacement.shuffle(seq)
: Shuffles the elements in a sequence randomly.seed(a=None)
: Initializes the random number generator with a seed value (default is the system time).
These functions can be useful in a variety of applications, such as generating random passwords, shuffling a deck of cards, or selecting a random sample from a population.