What's the shortest way to generate a random IP address in Ruby? What's the shortest way to generate a random IP address in Ruby? ruby ruby

What's the shortest way to generate a random IP address in Ruby?


You could use IPAddr

require 'ipaddr'ipv4 = IPAddr.new(rand(2**32),Socket::AF_INET)ipv6 = IPAddr.new(rand(2**128),Socket::AF_INET6)


If you want a truly random IP address, Array.new(4){rand(256)}.join('.') does it


I would suggest using Faker

https://github.com/stympy/faker#fakerinternet

Faker::Internet.ip_v4_address #=> "24.29.18.175"