Skip to main content

Random Module

Random Module in Python

Random module in Python is a widely used library that allows developers to generate random numbers, shuffle lists, and make other random selections for their applications. It provides a range of functions that can be used to create random data and can be useful in various applications like gaming, simulations, cryptography and other areas where there is a need for randomness. With this module, developers have the freedom to create diversity in their application output, thus making the applications more intriguing.

Random Module Usage Examples

The random module (random library) in Python is used to generate pseudo-random numbers.

Get Random Number

import random

# Generate a random number between 1 and 10 (inclusive)
random_number = random.randint(1, 10)
print(random_number)

# Output:
# 5 (this number will be different each time the code is run)

In the above example, the random.randint(a, b) function is used to generate a random integer between the values of a and b, inclusive of both endpoints. In this case, the random number generated will be between 1 and 10.

Random Select from List

import random

# Generate a random choice from a list of options
options = ["rock", "paper", "scissors"]
random_option = random.choice(options)
print(random_option)

# Output:
# "paper" (this option will be different each time the code is run)

In this example, the random.choice(seq) function is used to generate a random choice from a list of options. In this case, the random choice generated will be one of the three options in the options list - rock, paper, or scissors.

Random Seed

Random Seed plays a crucial role in generating random numbers in Python. It is used to initialize the pseudo-random number generator, which is a function that generates a sequence of numbers that appear to be random, but are actually deterministic.

import random

# Setting the random seed value to 123
random.seed(123)

# Generating 5 random numbers
for i in range(5):
    print(random.random())

In this example, we set the random seed value to 123, which ensures that the same sequence of random numbers is generated every time the code is run.

import random

# Setting different random seed values
random.seed(123)
list_1 = [random.randint(1,10) for i in range(5)]

random.seed(456)
list_2 = [random.randint(1,10) for i in range(5)]

# Printing the lists
print(list_1)
print(list_2)

In this example, we set different random seed values to generate two different lists. By doing this, we ensure that the lists are reproducible and can be generated again with the same random seed values.

Overall, random seed is an essential feature in generating and reproducing random numbers in Python. By setting the seed, we can ensure that our results are consistent and reproducible.

How to generate a Random String

If you're looking to generate a random string in Python, there are a few different methods you can use depending on your needs. Here are two examples:

import random
import string

def random_string(length):
    letters = string.ascii_lowercase
    return ''.join(random.choice(letters) for i in range(length))

# Example usage
print(random_string(10))  # Output: 'gjwstqyeqh'

In this example, we use Python's built-in random and string modules to generate a string of a given length. We first define a random function called random_string which takes a length parameter that specifies the number of characters in the resulting string. We then use the string.ascii_lowercase constant to get all lowercase letters in the alphabet, and random.choice to randomly select characters from this string for the length of our specified string.

import random

# random function

def random_string(chars, length):
    return ''.join(random.choice(chars) for i in range(length))

# Example usage
print(random_string('ABC123', 8))  # Output: '31C1C1CA'

If you want more control over the characters in the resulting string, you can specify a set of characters to choose from. In this example, we define our own set of characters to choose from by passing in a string of possible characters to the random function random_string. We then use random.choice to randomly select characters from this set for the length of our specified string.

With these two examples, you should be able to easily generate random strings in Python for whatever purpose you need. Remember to import the necessary modules and choose the appropriate method based on your specific requirements.

Random Sampling

Random sampling is an important statistical technique used by data analysts and programmers to select a subset of data from a larger population in a random manner. In Python, random sampling can be done using the built-in random module. This article will briefly discuss how to generate random samples in Python.

Random Sampling in Python

In Python, the random module provides several functions to generate random data. To generate a random sample from a list, we can use the random.sample() function. This function takes two arguments: the list from which we want to generate a random sample and the number of samples we want to generate.

import random

mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# Generate a random sample of size 4 from the list
random.sample(mylist, 4)

# Output: [9, 1, 5, 4]
import random

# Generate a random floating-point number between 0 and 1
random.random()
# Output: `0.42323281409391506`

Random sampling is a powerful technique that can be used to select subsets of data for analysis and modeling. In Python, the random module makes it easy to generate random samples from lists and other data sources. By using the random.sample() function, we can quickly generate random shuffle list of a specified size.

Random with the randint() Method

Python's Random randint() method is used to generate a random integer between the given range of values. The randint() function is part of the random module in Python.

Here is an example of how to use randint() to generate a random number between 0 and 9:

import random

number = random.randint(0, 9)
print(number)

This code will output a random integer between 0 and 9 (inclusive).

You can also use randint() to generate a random number within a larger range. For example, to generate a random number between 100 and 200 (inclusive), you would use the following code:

import random

number = random.randint(100, 200)
print(number)

In summary, Python's random.randint() is a simple and efficient way to generate random integers, and it is a useful tool in many programming projects.

Random with the uniform() Function

The random.uniform() is a Python function that generates random floating-point numbers within a specified range. It is a part of the random module, which is used for generating random data.

Using random.uniform() function in Python is relatively easy.

Generate a Random Float

import random

x = random.uniform(0, 1)
print(x)

The random.uniform() function generates a random floating-point number within the specified range. The range is between 0 and 1.

Using random.uniform() function is especially useful when one needs to generate random floating-point numbers that are not integers. When combined with other functions and modules, it can be used to simulate real-world scenarios such as financial modeling, physics simulations, and modeling of biological systems in computational biology.

Contribute with us!

Do not hesitate to contribute to Python tutorials on GitHub: create a fork, update content and issue a pull request.

Profile picture for user AliaksandrSumich
Python engineer, expert in third-party web services integration.
Updated: 05/03/2024 - 21:53
Profile picture for user angarsky
Reviewed and approved