Generate Random IP Addresses in C++
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: C++
What: Generate random IPv4 and IPv6 addresses in C++ using std::random and string formatting. Includes private IP ranges, validation, and network testing utilities.
Try it: Use our interactive Ip-addresses generator or integrate this code into your C++ application.
Generate random IPv4 and IPv6 addresses in C++ using std::random and string formatting. Includes private IP ranges, validation, and network testing utilities. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C# , Ruby and Go or use our interactive web generator.
C++ Code Example
#include <iostream>
#include <random>
#include <sstream>
#include <iomanip>
#include <vector>
#include <regex>
class IpAddressGenerator {
private:
std::mt19937 rng;
std::uniform_int_distribution<int> octet_dist;
std::uniform_int_distribution<int> hex_dist;
public:
IpAddressGenerator() :
rng(std::random_device{}()),
octet_dist(0, 255),
hex_dist(0, 0xFFFF) {}
// Generate random IPv4 address
std::string randomIpv4() {
std::ostringstream oss;
oss << octet_dist(rng) << "."
<< octet_dist(rng) << "."
<< octet_dist(rng) << "."
<< octet_dist(rng);
return oss.str();
}
// Generate random private IPv4 (RFC 1918)
std::string randomPrivateIpv4() {
std::uniform_int_distribution<int> range_dist(0, 2);
int range = range_dist(rng);
std::ostringstream oss;
switch (range) {
case 0: // 10.0.0.0/8
oss << "10." << octet_dist(rng) << "."
<< octet_dist(rng) << "." << octet_dist(rng);
break;
case 1: // 172.16.0.0/12
std::uniform_int_distribution<int> range172(16, 31);
oss << "172." << range172(rng) << "."
<< octet_dist(rng) << "." << octet_dist(rng);
break;
default: // 192.168.0.0/16
oss << "192.168." << octet_dist(rng) << "." << octet_dist(rng);
break;
}
return oss.str();
}
// Check if IP is private
bool isPrivateIp(const std::string& ip) {
std::regex private_patterns[] = {
std::regex("^10\\."),
std::regex("^172\\.(1[6-9]|2[0-9]|3[01])\\."),
std::regex("^192\\.168\\."),
std::regex("^127\\."),
std::regex("^0\\.")
};
for (const auto& pattern : private_patterns) {
if (std::regex_search(ip, pattern)) {
return true;
}
}
return false;
}
// Generate random public IPv4
std::string randomPublicIpv4() {
std::string ip;
do {
ip = randomIpv4();
} while (isPrivateIp(ip));
return ip;
}
// Generate random IPv6 address
std::string randomIpv6() {
std::ostringstream oss;
for (int i = 0; i < 8; ++i) {
if (i > 0) oss << ":";
oss << std::hex << std::setw(4) << std::setfill('0')
<< hex_dist(rng);
}
return oss.str();
}
// Generate IPv4 with CIDR notation
std::string randomIpv4WithCidr(int minCidr = 8, int maxCidr = 32) {
std::uniform_int_distribution<int> cidr_dist(minCidr, maxCidr);
std::ostringstream oss;
oss << randomIpv4() << "/" << cidr_dist(rng);
return oss.str();
}
// Generate multiple IP addresses
std::vector<std::string> generateIpAddresses(int count = 10, const std::string& type = "ipv4") {
std::vector<std::string> ips;
for (int i = 0; i < count; ++i) {
ips.push_back(type == "ipv6" ? randomIpv6() : randomIpv4());
}
return ips;
}
};
int main() {
IpAddressGenerator generator;
std::cout << "Random IPv4: " << generator.randomIpv4() << std::endl;
std::cout << "Private IPv4: " << generator.randomPrivateIpv4() << std::endl;
std::cout << "Public IPv4: " << generator.randomPublicIpv4() << std::endl;
std::cout << "IPv6: " << generator.randomIpv6() << std::endl;
std::cout << "IPv4 with CIDR: " << generator.randomIpv4WithCidr() << std::endl;
auto batch = generator.generateIpAddresses(5);
std::cout << "Batch: ";
for (size_t i = 0; i < batch.size(); ++i) {
if (i > 0) std::cout << ", ";
std::cout << batch[i];
}
std::cout << std::endl;
return 0;
}
[EXPLANATION]
This C++ implementation uses std::mt19937 Mersenne Twister for high-quality random generation and std::uniform_int_distribution for generating octets (0-255) and IPv6 groups (0-0xFFFF). std::ostringstream handles string formatting, with std::setw(4) and std::setfill('0') for zero-padded hexadecimal IPv6 groups. std::regex provides private IP pattern matching. For network applications, consider using Boost.Asio's boost::asio::ip::address for validation.
Expected Output
Random IPv4: 198.51.100.42 Private IPv4: 192.168.15.234 Public IPv4: 203.0.113.89 IPv6: 2001:0db8:85a3:0000:0000:8a2e:0370:7334 IPv4 with CIDR: 198.51.100.42/24 Batch: 203.0.113.15, 198.51.100.89, 192.0.2.45, 198.51.100.123, 203.0.113.67
Common Use Cases
- Generate test data for network simulation applications
- Create mock IP addresses for C++ network libraries
- Test IP-based routing and filtering algorithms
- Populate network configuration structures with test data
- Generate sample data for network security tools
Important Notes
-
std::mt19937provides better quality randomness thanrand() -
Boost.Asio provides
boost::asio::ip::address::from_string()for validation -
std::regexrequires C++11; use Boost.Regex for older compilers -
For IPv6,
std::setw(4)ensures zero-padding (e.g., "0042" not "42") -
Consider Qt's
QHostAddressfor cross-platform IP address handling
Try Our Interactive Generator
Don't want to write code? Use our free web-based Ip-addresses generator with instant results.
TRY IP-ADDRESSES GENERATOR →Other Programming Languages
View Ip-addresses generation code examples in PHP
View Ip-addresses generation code examples in JavaScript
View Ip-addresses generation code examples in Python
View Ip-addresses generation code examples in Java
View Ip-addresses generation code examples in C#
View Ip-addresses generation code examples in Ruby
View Ip-addresses generation code examples in Go