[Home] > Snippets  > Languages  > JavaScript  > Random  >  Generate a random IP address

Generate a random IP address

JavaScript

const randomIp = () =>
Array(4)
.fill(0)
.map((_, i) => Math.floor(Math.random() * 255) + (i === 0 ? 1 : 0))
.join('.')

TypeScript

const randomIp = (): number =>
Array(4)
.fill(0)
.map((_, i) => Math.floor(Math.random() * 255) + (i === 0 ? 1 : 0))
.join('.')

Examples

randomIp() // 175.89.174.131