from random import randrange k = 20 trials = 1000 totalsteps = 0 # total number of steps over all trials minsteps = None # smallest number of steps in a single trial maxsteps = None # biggest number of steps in a single trial for t in range(trials): position = 0 count = 0 while abs(position) < k: count += 1 position += -1 + 2*randrange(2) # will be either -1 or +1 totalsteps += count if t == 0: # first trial minsteps = maxsteps = count else: minsteps = min(count, minsteps) maxsteps = max(count, maxsteps) print('Average number of steps', totalsteps/trials) print('Minimum number of steps', minsteps) print('Maximum number of steps', maxsteps)