Generate Random IP Addresses in Ruby
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: Ruby
What: Generate random IPv4 and IPv6 addresses in Ruby using rand and IPAddr. Includes validation, RFC 1918 private ranges, and integration with Faker gem for Rails testing.
Try it: Use our interactive Ip-addresses generator or integrate this code into your Ruby application.
Generate random IPv4 and IPv6 addresses in Ruby using rand and IPAddr. Includes validation, RFC 1918 private ranges, and integration with Faker gem for Rails testing. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C# , C++ and Go or use our interactive web generator.
Ruby Code Example
require 'ipaddr'
# Generate random IPv4 address
def random_ipv4
Array.new(4) { rand(0..255) }.join('.')
end
# Generate random private IPv4 (RFC 1918)
def random_private_ipv4
ranges = [
-> { "10.#{rand(0..255)}.#{rand(0..255)}.#{rand(0..255)}" },
-> { "172.#{rand(16..31)}.#{rand(0..255)}.#{rand(0..255)}" },
-> { "192.168.#{rand(0..255)}.#{rand(0..255)}" }
]
ranges.sample.call
end
# Check if IP is private
def private_ip?(ip)
return true if ip.start_with?('10.')
return true if ip.match?(/^172\.(1[6-9]|2[0-9]|3[01])\./)
return true if ip.start_with?('192.168.')
return true if ip.start_with?('127.')
return true if ip.start_with?('0.')
false
end
# Generate random public IPv4
def random_public_ipv4
loop do
ip = random_ipv4
return ip unless private_ip?(ip)
end
end
# Generate random IPv6 address
def random_ipv6
Array.new(8) { rand(0..0xFFFF).to_s(16).rjust(4, '0') }.join(':')
end
# Generate IPv4 with CIDR notation
def random_ipv4_with_cidr(min_cidr: 8, max_cidr: 32)
ip = random_ipv4
cidr = rand(min_cidr..max_cidr)
"#{ip}/#{cidr}"
end
# Validate IP address using IPAddr
def valid_ip?(ip_string)
IPAddr.new(ip_string)
true
rescue IPAddr::InvalidAddressError
false
end
# Check if IP is private using IPAddr
def private_ip_addr?(ip_string)
ip = IPAddr.new(ip_string)
ip.private?
rescue IPAddr::InvalidAddressError
false
end
# Generate multiple IP addresses
def generate_ip_addresses(count = 10, type: :ipv4)
count.times.map do
type == :ipv6 ? random_ipv6 : random_ipv4
end
end
# Using Faker gem (add to Gemfile: gem 'faker')
# require 'faker'
# ipv4 = Faker::Internet.ip_v4_address
# ipv4_private = Faker::Internet.private_ip_v4_address
# ipv4_public = Faker::Internet.public_ip_v4_address
# ipv6 = Faker::Internet.ip_v6_address
# Usage examples
puts "Random IPv4: #{random_ipv4}"
puts "Private IPv4: #{random_private_ipv4}"
puts "Public IPv4: #{random_public_ipv4}"
puts "IPv6: #{random_ipv6}"
puts "IPv4 with CIDR: #{random_ipv4_with_cidr}"
puts "Batch: #{generate_ip_addresses(5).join(', ')}"
puts "Is valid: #{valid_ip?('192.168.1.1')}"
puts "Is private: #{private_ip_addr?('192.168.1.1')}"
[EXPLANATION]
Ruby's Array.new(4) { rand(0..255) } creates 4 random octets, joined with dots. The built-in IPAddr class provides validation and the private? method for detecting RFC 1918 addresses. rand(0..255) generates inclusive random numbers. For IPv6, to_s(16) converts to hexadecimal and rjust(4, '0') zero-pads to 4 digits. The Faker gem offers convenient methods like ip_v4_address, private_ip_v4_address, and ip_v6_address for Rails and RSpec testing.
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 Is valid: true Is private: true
Common Use Cases
- Generate test data for Rails network applications
- Create factory data with FactoryBot for IP fields
- Mock API responses in RSpec integration tests
- Test IP-based geolocation and routing features
- Populate databases with realistic network test data
Important Notes
-
IPAddris part of Ruby standard library, no gem installation needed -
IPAddr#private?automatically detects RFC 1918, loopback, and link-local addresses - Faker gem integrates seamlessly with FactoryBot for Rails testing
-
SecureRandomcan be used for cryptographically secure random bytes -
Use
IPAddr#maskfor subnet calculations and CIDR operations
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 C++
View Ip-addresses generation code examples in Go