I am new to Python and I'm trying to optimize this code for large numbers. However I'm struggling to find an optimized way. If I run it as it is, it takes almost 4 minutes. I know it has something to do with loop and the max and randint function. I tried to use random.random as I read that is quicker but I got almost the same result. Can you think of a better way so it doesn't take that long?
from random import randint
def func(iters, n):
# Function to add max random numbers to a list.
l = [0]
for i in range(iters):
r = randint(0, n)
max_l = max(l)
if r > max_l:
l.append(r)
else:
l.append(max_l + 1)
return l
func(100000, 50)