# Generate a random IP address

Return a random IPv4 address from the ranges reserved for documentation.

Canonical: https://1loc.dev/random/generate-a-random-ip-address/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Usage lines executed in Node.js 22 and headless Chromium; no external services
Updated: 2026-10-28

## Implementation

```javascript
function randomDocumentationIp() {
  const networks = ['192.0.2', '198.51.100', '203.0.113'];
  return `${networks[Math.floor(Math.random() * networks.length)]}.${Math.floor(Math.random() * 254) + 1}`;
}
```

## Usage

```javascript
const ip = randomDocumentationIp();
['192.0.2', '198.51.100', '203.0.113'].includes(ip.split('.').slice(0, 3).join('.')); // → true
randomDocumentationIp(); // e.g. '203.0.113.42'
```

Lines marked → were executed in Node.js 22 and in headless Chromium, and each result was compared with the value shown. Lines marked e.g. are illustrative.

## Limits

- RFC 5737 reserves 192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24 for documentation. They never belong to a real host, so fixtures and logs in examples cannot point at one.
- The original drew any address: Math.floor(Math.random() * 255) never produced 255, and results included private, loopback and multicast ranges.
- For IPv6 examples, RFC 3849 reserves 2001:db8::/32.

The function does not validate its arguments. Add checks where it receives untrusted input.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/generate-a-random-ip-address.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: PiyushSuthar, Comet32
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Draws from the RFC 5737 documentation ranges; the original never produced 255 and could return private, loopback or multicast addresses. Documented as a reference with executed usage lines. Original MDX retained as inert source evidence.
