Skip to content

Instantly share code, notes, and snippets.

@Pymmdrza
Created August 14, 2025 01:37
Show Gist options
  • Select an option

  • Save Pymmdrza/09795e7f5ac082330a4f11419242dd73 to your computer and use it in GitHub Desktop.

Select an option

Save Pymmdrza/09795e7f5ac082330a4f11419242dd73 to your computer and use it in GitHub Desktop.
IPv4 Generated from range octet
import random
from typing import Sequence
# Default per-octet inclusive ranges for the generated IPv4 address
OCTET_RANGES: tuple[tuple[int, int], ...] = (
(31, 245),
(31, 245),
(33, 245),
(66, 245),
)
def generate_ipv4(ranges: Sequence[tuple[int, int]] = OCTET_RANGES) -> str:
"""
Generate a random IPv4 address using per-octet inclusive ranges.
"""
octets = [random.randint(low, high) for (low, high) in ranges]
return ".".join(map(str, octets))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment