Last updated
IPv4 Address Structure
An IPv4 address is a 32-bit number written as four decimal octets separated by dots (e.g., 192.168.1.1). Each octet ranges from 0 to 255. The address space is divided into network and host portions by a subnet mask. Not all addresses are valid for public internet use — several ranges are reserved.
Reserved IPv4 Ranges
| Range | Purpose |
|---|---|
| 10.0.0.0/8 | Private network (Class A) |
| 172.16.0.0/12 | Private network (Class B) |
| 192.168.0.0/16 | Private network (Class C) |
| 127.0.0.0/8 | Loopback (localhost) |
| 169.254.0.0/16 | Link-local (APIPA) |
| 224.0.0.0/4 | Multicast |
| 255.255.255.255 | Broadcast |
Generating Random IPs in JavaScript
function randomIPv4() {
return Array.from({length: 4}, () => Math.floor(Math.random() * 256)).join('.');
}
function randomPublicIPv4() {
const RESERVED = [
[10,10], [127,127], [169,169], [172,172],
[192,192], [224,239], [240,255]
];
let ip;
do {
const octets = Array.from({length: 4}, () => Math.floor(Math.random() * 256));
ip = octets;
} while (RESERVED.some(([lo, hi]) => ip[0] >= lo && ip[0] <= hi));
return ip.join('.');
}
// Generate 10 random public IPs
for (let i = 0; i < 10; i++) {
console.log(randomPublicIPv4());
}
IPv6 Address Format
IPv6 addresses are 128-bit numbers written as eight groups of four hexadecimal digits
separated by colons (e.g., 2001:0db8:85a3:0000:0000:8a2e:0370:7334).
Leading zeros in each group can be omitted, and one consecutive sequence of all-zero
groups can be replaced with ::.