Generalise range function.