Hoping there's a simple way to do this.
I want to convert a range of numbers
(1..100)to an array with a limited index that distributes the range's numbers equally.
[1, 50, 100][1, 25, 50, 75, 100]I want to do this without having to know the exact number of values within the range.
Edit: Nevermind! Figured it out
#-------------------------------------------------------------------------- # New Method: distribute #-------------------------------------------------------------------------- def distribute(begin_value, end_value, array_size, return_ints = false) diff = 1.0 * (end_value - begin_value) n = [array_size-1, 1].max (0..(array_size-1)).map do |i| v = begin_value + i * diff / n return_ints ? v.round : v end end # distributebegin_value = First value in array
end_value = Last value in array
array_size = Maximum number of indexes within array
return_ints = Round values to integer? true or false (optional)
Script Call
Code:
distribute(0, 100, 5, true)# => [0, 25, 50, 75, 100]