Generate Random MAC Addresses in Ruby
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: Ruby
What: Generate random MAC addresses in Ruby using rand and Faker gem. Covers standard format, locally administered addresses, vendor prefixes, and validation for Rails/RSpec testing.
Try it: Use our interactive Mac-addresses generator or integrate this code into your Ruby application.
Generate random MAC addresses in Ruby using rand and Faker gem. Covers standard format, locally administered addresses, vendor prefixes, and validation for Rails/RSpec 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 "securerandom"
# Generate random MAC address (standard format)
def random_mac_address
Array.new(6) { rand(0..255) }.map { |octet| "%02X" % octet }.join(":")
end
# Generate locally administered MAC address (bit 1 of first octet set)
def random_local_mac_address
# First octet: set bit 1 (locally administered), clear bit 0 (unicast)
first_octet = (rand(0..127) & 0xFE) | 0x02
octets = [first_octet] + Array.new(5) { rand(0..255) }
octets.map { |octet| "%02X" % octet }.join(":")
end
# Generate MAC with specific vendor prefix (OUI)
def random_mac_with_vendor(vendor_prefix = "00:1A:2B")
vendor = vendor_prefix.split(":")
remaining = Array.new(6 - vendor.length) { "%02X" % rand(0..255) }
(vendor + remaining).join(":")
end
# Generate MAC with different separator
def random_mac_formatted(separator = ":")
Array.new(6) { "%02X" % rand(0..255) }.join(separator)
end
# Validate MAC address format
def valid_mac_address?(mac)
patterns = [
/^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$/, # 00:1A:2B:3C:4D:5E
/^([0-9A-Fa-f]{2}-){5}[0-9A-Fa-f]{2}$/, # 00-1A-2B-3C-4D-5E
/^[0-9A-Fa-f]{12}$/, # 001A2B3C4D5E
]
patterns.any? { |pattern| mac.match?(pattern) }
end
# Generate multiple MAC addresses
def generate_mac_addresses(count = 10)
count.times.map { random_mac_address }
end
# Using Faker gem (add to Gemfile: gem 'faker')
# require 'faker'
# mac = Faker::Internet.mac_address
# mac_dash = Faker::Internet.mac_address(prefix: "", separator: "-")
# Usage examples
puts "Random MAC: #{random_mac_address}"
puts "Local MAC: #{random_local_mac_address}"
puts "Vendor MAC: #{random_mac_with_vendor('00:1A:2B')}"
puts "Dash format: #{random_mac_formatted('-')}"
puts "No separator: #{random_mac_formatted('')}"
puts "Is valid: #{valid_mac_address?('00:1A:2B:3C:4D:5E')}"
puts "Batch: #{generate_mac_addresses(3).join(', ')}"
[EXPLANATION]
Ruby's rand(0..255) generates random octets, and "%02X" % octet formats as 2-digit uppercase hexadecimal with zero-padding. Array.new(6) { } creates arrays with block initialization. Locally administered MACs use bitwise operations (& 0xFE and | 0x02). match?() checks regex patterns efficiently. Faker gem provides Internet.mac_address with customizable prefix and separator for realistic test data in Rails, RSpec, and FactoryBot.
Expected Output
Random MAC: A3:45:B7:89:CD:EF Local MAC: 02:45:B7:89:CD:EF Vendor MAC: 00:1A:2B:89:CD:EF Dash format: A3-45-B7-89-CD-EF No separator: A345B789CDEF Is valid: true Batch: 12:34:56:78:9A:BC, DE:F0:12:34:56:78, 9A:BC:DE:F0:12:34
Common Use Cases
- Generate MAC addresses for Rails network testing applications
- Create device identifiers with FactoryBot for RSpec tests
- Populate database seeds with network interface test data
- Test MAC validation in Ruby network management APIs
- Generate locally administered MACs for Docker containers
Important Notes
-
"%02X"format string means 2-digit uppercase hex with zero-padding -
Faker's
mac_addressacceptsprefixandseparatoroptions -
SecureRandom.hex(6)can generate cryptographically secure random bytes -
match?()is more efficient than=~when you don't need capture groups - Locally administered bit (0x02) in first octet avoids hardware MAC conflicts
Try Our Interactive Generator
Don't want to write code? Use our free web-based Mac-addresses generator with instant results.
TRY MAC-ADDRESSES GENERATOR →Other Programming Languages
View Mac-addresses generation code examples in PHP
View Mac-addresses generation code examples in JavaScript
View Mac-addresses generation code examples in Python
View Mac-addresses generation code examples in Java
View Mac-addresses generation code examples in C#
View Mac-addresses generation code examples in C++
View Mac-addresses generation code examples in Go