Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/mac-addresses/python$ _

Generate Random MAC Addresses in Python

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: Python

What: Generate random MAC addresses in Python using random and Faker library. Covers standard format, locally administered addresses, vendor prefixes, and validation.

Try it: Use our interactive Mac-addresses generator or integrate this code into your Python application.

Generate random MAC addresses in Python using random and Faker library. Covers standard format, locally administered addresses, vendor prefixes, and validation. Looking for other languages? Check our code examples in PHP , JavaScript , Java , C# , C++ , Ruby and Go or use our interactive web generator.

Python Code Example

import random

# Generate random MAC address (standard format)
def random_mac_address():
    octets = [random.randint(0, 255) for _ in range(6)]
    return ":".join(f"{octet:02X}" for octet in octets)

# 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 = (random.randint(0, 127) & 0xFE) | 0x02
    octets = [first_octet] + [random.randint(0, 255) for _ in range(5)]
    return ":".join(f"{octet:02X}" for octet in octets)

# Generate MAC with specific vendor prefix (OUI)
def random_mac_with_vendor(vendor_prefix="00:1A:2B"):
    vendor = vendor_prefix.split(":")
    octets = [int(octet, 16) for octet in vendor]

    for _ in range(6 - len(octets)):
        octets.append(random.randint(0, 255))

    return ":".join(f"{octet:02X}" for octet in octets)

# Generate MAC with different separator
def random_mac_formatted(separator=":"):
    octets = [random.randint(0, 255) for _ in range(6)]
    return separator.join(f"{octet:02X}" for octet in octets)

# Validate MAC address format
def is_valid_mac_address(mac):
    import re
    patterns = [
        r"^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$",  # 00:1A:2B:3C:4D:5E
        r"^([0-9A-Fa-f]{2}-){5}[0-9A-Fa-f]{2}$",  # 00-1A-2B-3C-4D-5E
        r"^[0-9A-Fa-f]{12}$",                      # 001A2B3C4D5E
    ]

    return any(re.match(pattern, mac) for pattern in patterns)

# Generate multiple MAC addresses
def generate_mac_addresses(count=10):
    return [random_mac_address() for _ in range(count)]

# Using Faker library (install: pip install faker)
# from faker import Faker
# fake = Faker()
# mac = fake.mac_address()  # Format: XX:XX:XX:XX:XX:XX

# Usage examples
print("Random MAC:", random_mac_address())
print("Local MAC:", random_local_mac_address())
print("Vendor MAC:", random_mac_with_vendor("00:1A:2B"))
print("Dash format:", random_mac_formatted("-"))
print("No separator:", random_mac_formatted(""))
print("Is valid:", is_valid_mac_address("00:1A:2B:3C:4D:5E"))
print("Batch:", generate_mac_addresses(3))

[EXPLANATION]

Python's random.randint(0, 255) generates random octets, and f-strings with {octet:02X} format as 2-digit uppercase hexadecimal. List comprehensions create octet arrays efficiently. Locally administered MACs use bitwise operations (& 0xFE clears bit 0, | 0x02 sets bit 1). The Faker library provides fake.mac_address() for realistic MAC generation. Validation uses re.match() with any() to check multiple patterns. Format specifier :02X means 2-digit uppercase hex with zero-padding.

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 network testing in Django/Flask applications
  • Create device identifiers for IoT simulation and testing
  • Populate network databases with test MAC addresses
  • Test MAC validation in network management APIs
  • Generate locally administered MACs for virtual network interfaces

Important Notes

  • f-string format {octet:02X} means 2-digit uppercase hex with zero-padding
  • Faker's mac_address() always generates colon-separated format
  • For cryptographic randomness, use secrets.randbelow(256)
  • Locally administered bit (0x02) in first octet avoids hardware conflicts
  • OUI lookup databases can validate vendor prefixes against real manufacturers

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 →