Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,401 questions

56,365 answers

573 users

How to generate an 8 digits random number in Python

4 Answers

0 votes
import random

# Loop 10 times (from 0 to 9)
for _ in range(10):
    # randint(a, b) includes both a and b in the result
    print(random.randint(10000000, 99999999))


'''
run:

88179460
98272315
52986914
66839579
22976953
74939267
41631158
91927078
56242782
38069499

'''

 



answered Apr 27, 2016 by avibootz
edited 2 days ago by avibootz
0 votes
import random

for _ in range(10):
    # randrange(start, stop) excludes stop (up to 99999999)
    print(random.randrange(10000000, 100000000))


'''
run:

15582984
19602361
60041031
81815647
31038704
71567823
67068698
30742625
15024457
39988333

'''

 



answered 2 days ago by avibootz
0 votes
import random

for _ in range(10):
    code = f"{random.randint(0, 99999999):08d}"
    print(code)


'''
run:

13340783
12930645
43404663
83906349
42785283
66806496
26424310
45802374
73368159
64055043

'''

 



answered 2 days ago by avibootz
0 votes
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

'''

 



answered 2 days ago by avibootz
...