How to Generate Random IP Addresses? Principles Explained with Code Examples

In scenarios such as web development, system testing, and data simulation, it is often necessary to generate random IP addresses. Many people’s first reaction is: can’t you just randomly write a few numbers? But in real-world use, you’ll quickly find that random IPs are far more complex than they seem: being able to generate one doesn’t mean it’s reasonable; looking like an IP doesn’t mean it’s usable.
What Is an IP Address?
Taking IPv4 as an example, an IP address consists of four numbers ranging from 0 to 255, such as:
192.168.1.1
8.8.8.8In theory, IPv4 has about 4.3 billion addresses, but that doesn’t mean all of them can be freely used.
Depending on their purpose, IP addresses can generally be divided into three categories:
· Public IPs: routable and accessible on the Internet
· Private IPs: used only within local networks
· Reserved / Special IPs: loopback, broadcast, and testing purposes
“Random IP” usually refers to a randomly generated public IP address.
The Simplest Way to Generate a Random IP (Basic Version)
The most common and intuitive approach is:
Randomly generate four integers between 0–255
Join them into a string using .
Example (Python)
import random
def random_ip_basic():
return ".".join(str(random.randint(0, 255)) for _ in range(4))
for _ in range(5):
print(random_ip_basic())From a formatting perspective, the generated string fully conforms to the IPv4 format.
⚠️ Where’s the Problem?
This approach may generate private IPs (such as 192.168.x.x), local loopback IPs (127.x.x.x), non-routable addresses, or IPs that simply don’t exist or have long been blocked.
Filtering Out Invalid IPs
1. Common IPv4 Ranges That Should Be Excluded
| IP Range | Purpose |
|---|---|
| 10.0.0.0 – 10.255.255.255 | Private network |
| 172.16.0.0 – 172.31.255.255 | Private network |
| 192.168.0.0 – 192.168.255.255 | Private network |
| 127.0.0.0 – 127.255.255.255 | Loopback |
| 0.0.0.0 | Special address |
2. Common IPv6 Address Ranges That Should Be Excluded
| IPv6 Prefix | Purpose |
|---|---|
| ::1/128 | Loopback address |
| ::/128 | Unspecified address |
| fe80::/10 | Link-local address (local network only) |
| fc00::/7 | Unique Local Address (ULA, similar to private IPs) |
| ff00::/8 | Multicast address |
| 2001:db8::/32 | Documentation / example address (not routable) |
The Standard Way to Generate Random IP Addresses: Using the ipaddress Standard Library
It is recommended to use Python’s built-in ipaddress library to handle IP addresses.
This is an officially maintained Python standard library that provides built-in support for identifying various IPv4 and IPv6 address properties, making it far more reliable than writing custom rules by hand.
Example:
IPv4:
import random
import ipaddress
def random_public_ipv4():
while True:
ip = ipaddress.IPv4Address(random.randint(0, 2**32 - 1))
if ip.is_global:
return str(ip)
for _ in range(5):
print(random_public_ipv4())IPv6:
import random
import ipaddress
def random_public_ipv6():
while True:
ip = ipaddress.IPv6Address(random.randint(0, 2**128 - 1))
if ip.is_global:
return str(ip)
for _ in range(5):
print(random_public_ipv6())The key point here is:
is_global == True
This means the IP address:
Is not a private address, not a loopback address, not a reserved address, and is theoretically routable on the public internet.
Real-World Use Cases: Using Proxy IPs
If your goals include:
🌍 Simulating different countries or regions
🤖 Web scraping or data collection
🔐 Risk control / anti-fraud testing
🧪 Multi-account environment isolation
Then what you need is not an IP generated by a random algorithm, but:
A real, reachable Proxy IP
Such IPs typically have the following characteristics:
· They actually exist on the internet
· They can connect to the network normally
· They have attributes such as country, ISP, and ASN
· They appear more like real user networks to systems
Summary
Generating random IP addresses is not as simple as stitching together a few numbers. A truly reasonable random IP must follow IP allocation rules and clearly distinguish between public, private, and reserved addresses. In Python, using the officially provided ipaddress standard library allows you to generate structurally valid public IPs in a more standardized and reliable way, suitable for testing and data simulation scenarios.
IPDeep provides high-quality proxy IP services, including:
Along with many other proxy types, IPDeep offers over 10 million high-quality IP resources, covering 200+ countries and regions worldwide, helping you efficiently and securely complete various business tasks in complex network environments.




