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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

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

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,970 questions

51,912 answers

573 users

How to select random two digits from anywhere in a number with Python

1 Answer

0 votes
import random
import time

def get_random_two_digits(number: int) -> str:
    num_str = str(number)

    if len(num_str) < 2:
        return "Error: number must have at least 2 digits"

    # Generate two distinct random indices
    i = random.randrange(len(num_str))
    j = i
    while j == i:
        j = random.randrange(len(num_str))

    # Form the two-digit string
    return num_str[i] + num_str[j]

def main():
    random.seed(time.time())

    num = 1234567
    random_two = get_random_two_digits(num)

    print("Random two digits:", random_two)

if __name__ == "__main__":
    main()




'''
run:

Random two digits: 53

'''

 



answered Nov 26, 2025 by avibootz
...