Search code examples
pythonlistrandomnumbers

How to create list of 15-digit numbers with condition?


I want to create list of random 20 15-digit numbers and each of this 15-digit numbers must follow 1 rule.

The rule is that I want this 15-digit number to be made out of 5 3-digit numbers in which first digit + second digit = third digit or if sum of first two digits is greater than 10 then third digit must be, equal to second digit of sum of first two digits. for example if first digit is 5 and second is 8, third digit must be 3 since 5 + 8 = 13.

I've written code that fills list with 15-digit numbers with the same rule, but it only works for first three digits.

import random as rd
def numchoose(start, end):
arr=[]
num=0
while num<20:
   a=(rd.randint(start, end))
   if int(str(a)[0]) + int(str(a)[1]) == int(str(a)[2]):
       arr.append(a)
       num+=1
   elif int(str(a)[0]) + int(str(a)[1]) > 10 and int(str(a)[2]) == int(str(int(str(a)[0]) +
int(str(a)[1]))[1])  :
      arr.append(a)
      num+=1
   else: continue


print(numchoose(100000000000000, 999999999999999))

How do I write this code so that entire 15-digit number is made out of 3-digit numbers that follow the stated rule and first three digits are not the only ones that follow rule?


Solution

  • This seems to work, but i replaced the big number with how long you want the number to be.

    import random as rd
    
    def numchoose(len):
        number = ""
        for i in range(int(len/3)):
            a = rd.randint(0, 9)
    
            while i == 0 and a == 0:
                a = rd.randint(0, 9)
    
            b = rd.randint(0, 9)
            c = a + b
    
            if c >= 10:
                c -= 10
    
            number += str(a) + str(b) + str(c)
    
        return int(number)
    
    print(numchoose(15))