import random
import secrets
def generate_8_digit_number() -> int:
"""Generates a random 8-digit integer between 10,000,000 and 99,999,999 (inclusive).
Guaranteed to start with a non-zero digit (1-9). Suitable for standard mathematical
or statistical uses.
Returns:
int: An 8-digit positive integer.
"""
# random.randint(a, b) includes both endpoints 'a' and 'b'
return random.randint(10_000_000, 99_999_999)
def generate_8_digit_code(secure: bool = False) -> str:
"""Generates an 8-character numeric string between "00000000" and "99999999".
Preserves leading zeros, making it suitable for OTPs, PINs, or tracking IDs.
Args:
secure (bool): If True, uses the cryptographically secure 'secrets' module
instead of the pseudo-random 'random' module.
Returns:
str: An 8-character numeric string padded with leading zeros if necessary.
"""
if secure:
# secrets.randbelow(n) returns a cryptographically secure random int in range [0, n)
raw_value = secrets.randbelow(100_000_000)
else:
raw_value = random.randint(0, 99_999_999)
# Format as an 8-digit decimal string with leading zero padding
return f"{raw_value:08d}"
def main() -> None:
# 1. Standard pseudo-random 8-digit integer
number = generate_8_digit_number()
print(f"Generated 8-digit integer: {number}")
# 2. Standard 8-digit string code (preserves leading zeros)
code = generate_8_digit_code()
print(f"Generated 8-digit string code: {code}")
# 3. Cryptographically secure 8-digit string code (for security tokens/OTPs)
secure_code = generate_8_digit_code(secure=True)
print(f"Generated secure 8-digit OTP code: {secure_code}")
if __name__ == "__main__":
main()
'''
run:
Generated 8-digit integer: 27548668
Generated 8-digit string code: 28206596
Generated secure 8-digit OTP code: 65969631
'''