Spaces:
Running
Running
# | |
# SPDX-FileCopyrightText: Hadad <[email protected]> | |
# SPDX-License-Identifier: Apache-2.0 | |
# | |
import random # Import the random module to generate random numbers | |
def generate_ip() -> str: | |
""" | |
Generate a random IPv4 address as a string. | |
Returns: | |
str: A randomly generated IPv4 address in dotted decimal notation, | |
where each octet is a number between 1 and 254 inclusive. | |
Explanation: | |
This function creates an IP address by generating four random integers, | |
each representing one octet of the IP address. | |
The range 1 to 254 is used to avoid special addresses like 0 (network) and 255 (broadcast). | |
The four octets are then joined together with dots to form a standard IPv4 address string. | |
""" | |
# Generate four random integers between 1 and 254 inclusive, convert each to string, | |
# then join them with '.' to form a valid IPv4 address string | |
return ".".join(str(random.randint(1, 254)) for _ in range(4)) |