4
votes

Specific instance of Problem
I have an int range from 1-100. I want to generate n total numbers within this range that are as evenly distributed as possible and include the first and last values.

Example

start = 1, end = 100, n = 5   
Output: [1, 25, 50, 75, 100]

start = 1, end = 100, n = 4   
Output: [1, 33, 66, 100]

start = 1, end = 100, n = 2   
Output: [1, 100]

What I currently have
I actually have a working approach but I keep feeling I am over thinking this and missing something more simple? Is this the most efficient approach or could this be improved?

def steps(start, end, n):
    n = min(end, max(n, 2) - 1)
    mult = end / float(n)
    yield start
    for scale in xrange(1, n+1):
        val = int(mult * scale)
        if val != start:
            yield val

Note, I am ensuring that this function will always return at least the lower and upper limit values of the range. So, I force n >= 2

Just for search reference, I am using this to sample image frames from a rendered sequence, where you would usually want the first, middle, last. But I wanted to be able to scale a bit better to handle really long image sequences and get better coverage.

Solved: From the selected answer

I ended up using this slightly modified version of @vartec's answer, to be a generator, and also cap the n value for safety:

def steps(start,end,n):
    n = min(end, max(n, 2))
    step = (end-start)/float(n-1)
    return (int(round(start+x*step)) for x in xrange(n))
5
Your code doesn't handle list(steps(50,100,3)) - jamylak
[1, 33, 66, 100] is not most even possible, it should be [1, 33, 67, 100], after all 66.66(6) should be rounded up, not down. - vartec
For example 2 your numbers are not evenly spaced (see my answer) or do I miss something? - bmu
@bmu: You didn't miss anything. I forgot to use round to make it even closer. - jdi
@vartec: You are right, I forgot to use round :-) - jdi

5 Answers

5
votes

You need proper rounding:

def steps(start,end,n):
    if n<2:
        raise Exception("behaviour not defined for n<2")
    step = (end-start)/float(n-1)
    return [int(round(start+x*step)) for x in range(n)]
4
votes

Extra dependency and maybe overkill, but short, tested and should give correct results: numpy.linspace

>>> numpy.linspace(1, 100, 4).astype(int).tolist()
[1, 34, 67, 100]
3
votes

The problem with using range is that the step must be an integer and so you get rounding issues, such as steps(1,100,4) == [1, 33, 66, 100]. If you want integer outputs but want as even a step as possible, use a float as your step.

>>> def steps(start,end,n):
...    step = (end-start)/float(n-1)
...    return [int(round(start+i*step)) for i in range(n)]

>>> steps(1,100,5)
>>> [1, 26, 51, 75, 100]
>>> steps(1,100,4)
>>> [1, 34, 67, 100]
>>> steps(1,100,2)
>>> [1, 100]
>>>
1
votes
>>> from itertools import count
>>> def steps(start,end,n):
        yield start
        begin = start if start>1 else 0
        c = count(begin,(end-begin)/(n-1))
        next(c)
        for _ in range(n-2):
            yield next(c)
        yield end


>>> list(steps(1,100,2))
[1, 100]
>>> list(steps(1,100,5))
[1, 25, 50, 75, 100]
>>> list(steps(1,100,4))
[1, 33, 66, 100]
>>> list(steps(50,100,3))
[50, 75, 100]
>>> list(steps(10,100,10))
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

Can be shortened to

>>> from itertools import islice, count
>>> def steps(start,end,n):
        yield start
        begin = start if start>1 else 0
        c = islice(count(begin,(end-begin)/(n-1)),1,None)
        for _ in range(n-2):
            yield next(c)
        yield end
0
votes

What's wrong with using range? Here is how you can use it

>>> def steps(start,end,n):
    return [start]+range(start-1,end,end/(n-1))[1:]+[end]

>>> steps(1,100,5)
[1, 25, 50, 75, 100]
>>> steps(1,100,2)
[1, 100]
>>>