Use xrange instead of range on python 2.

Closes #681.