vendredi 18 septembre 2020

Generating random numbers which do not include zeros with Python

I am trying to create a program which finds multiplicative persistence in numbers which are randomized. This program, however, has to deal with large numbers (>10233). However, when I use random.randint(10**233, 10**400), I cannot help but notice that the vast majority of numbers contain a zero, which would result in 0 multiplicative persistence. In my algorithm, I am attempting to get rid of these numbers with the following approach:

def find_new_i():
    i = 10
    while "0" in str(i):
        i = random.randint(10**233,10**400)
    return i

However, this approach is really slow, and cannot generate numbers very quickly.

Has anyone got any ideas on a more efficient approach?




Aucun commentaire:

Enregistrer un commentaire