Switch from xrange to range for Python 3 support

Obviously range() is not ideal for Python 2, but usually max_iter will be
small enough that it shouldn't be a problem. If needed itertools.repeat can be
used.