# How to generate a random IP address in Python

Last updated: Apr 10, 2024
3 min

## #Generate a random IP address in Python

To generate a random IP address:

1. Use the `random.randint()` method to generate a random number from `0` to `255`.
2. Generate a random number in the range `4` times.
3. Use the `str.join()` method to join the numbers with a period separator.
main.py
```Copied!```from random import randint

def generate_random_ip():
return '.'.join(
str(randint(0, 255)) for _ in range(4)
)

random_ip = generate_random_ip()
print(random_ip)  # ๐๏ธ 178.166.111.80

random_ip = generate_random_ip()
print(random_ip)  # ๐๏ธ 124.56.199.70
``````

We used a generator expression to iterate over a `range` object of length 4.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

The range() class is commonly used for looping a specific number of times.

main.py
```Copied!```print(list(range(4))) # ๐๏ธ [0, 1, 2, 3]
``````

On each iteration, we use the `random.randint()` method to generate a random number from `0` to `255`.

main.py
```Copied!```from random import randint

def generate_random_ip():
return '.'.join(
str(randint(0, 255)) for _ in range(4)
)

random_ip = generate_random_ip()
print(random_ip)  # ๐๏ธ 178.166.111.80
``````

The random.randint() function takes 2 numbers - `a` and `b` as parameters and returns a random integer in the range.

Note that the range is inclusive - meaning both `a` and `b` can be returned.

We used the `str()` class to convert each number to a string because the `str.join()` method can only be passed string values.

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a `TypeError` if there are any non-string values in the iterable.

The string the method is called on is used as the separator between the elements.

## #Generating a list of N IP addresses

You can use the `range()` class to generate a list of N IP addresses.

main.py
```Copied!```from random import randint

def generate_random_ip():
return '.'.join(
str(randint(0, 255)) for _ in range(4)
)

a_list = []

for _ in range(3):
a_list.append(generate_random_ip())

# ๐๏ธ ['123.31.155.255', '70.22.71.106', '183.142.245.87']
print(a_list)
``````

We used the `range` class to call the function 3 times and appended the IP addresses to a list.

Alternatively, you can use the `Faker` module.

## #Generate a random IP address using Faker

This is a three-step process:

1. Install the `Faker` module by running `pip install Faker`.
2. Use the `Faker` class to create a `Faker` object.
3. Use the `ipv4()` method to generate a random IP address.
main.py
```Copied!```from faker import Faker

fake = Faker()

print(ip_address)  # ๐๏ธ 173.188.6.176

print(ip_address)  # ๐๏ธ 54.45.106.198

print(ip_address)  # ๐๏ธ 10.103.230.160

print(ip_address)  # ๐๏ธ 120b:ecc2:fe2f:1db1:d79c:99e1:a960:3221
``````

Make sure to install the Faker module if you haven't.

main.py
```Copied!```pip install Faker

# ๐๏ธ or pip3
pip3 install Faker
``````

## #Generate random public or private IPv4 addresses

The `faker.ipv4()` method returns a random IPv4 address.

main.py
```Copied!```from faker import Faker

fake = Faker()

print(ip_address)  # ๐๏ธ 173.188.6.176
``````

The `faker.ipv4_public()` method returns a public IPv4 address excluding private blocks.

main.py
```Copied!```from faker import Faker

fake = Faker()

print(ip_address)  # ๐๏ธ 188.104.196.43
``````

The `faker.ipv4_private()` returns a private IPv4 address.

main.py
```Copied!```from faker import Faker

fake = Faker()

print(ip_address)  # ๐๏ธ 172.18.1.188
``````

## #Generate random IPv6 addresses

The `faker.ipv6()` method returns a random IPv6 address.

main.py
```Copied!```from faker import Faker

fake = Faker()