Generating evenly distributed multiples/samples within a range

前端 未结 5 1726
说谎
说谎 2020-12-21 05:47

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

5条回答
  •  醉话见心
    2020-12-21 06:16

    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)]
    

提交回复
热议问题