Q:

Evenly distributed integers within a range

Lets say I have a range between 0 and 100 and I want an array returned containing 3 integers which are evenly distributed within that range, what would be the best way to do this?
For example:
Range: 0-100
Wanted: 3
Returned: 25, 50, 75

A:

you can make use of array_chunk(), eg only
$end=100;
$a = range(0,$end);
$chunk=3;
foreach (array_chunk($a,$end/($chunk+1)) as $s){
     print $s[0]."\n";
}

output
$ php test.php
0
25
50
75
100

you can get rid of the start (0) and end(100) points if not needed.

A:

Pseudo code:
function foo(int rangeLow, int rangeHigh, int wanted)
    int increment=(rangeHigh-rangeLow)/(wanted+1)
    array r=new array()
    for (int i=rangeLow+increment;i<rangeHigh;i+=increment)
        r.push(i)
    return r

edit: missed the php tag...
//tested:
function foo($wanted=3, $rangeLow=0, $rangeHigh=100){
    $increment=($rangeHigh-$rangeLow)/($wanted+1);
    $r=array();
    for ($i=$rangeLow+$increment;$i<$rangeHigh;$i+=$increment)
        $r[]=$i;
    return $r;
}
/*
  examples:

  call:
      foo ();
  returned:
             [0] => 25
             [1] => 50
             [2] => 75

  call:
      foo (4);
  returned:
             [0] => 20
             [1] => 40
             [2] => 60
             [3] => 80

  call:
      foo (5,50,200);
  returned:
             [0] => 75
             [1] => 100
             [2] => 125
             [3] => 150
             [4] => 175
*/

A:

Here's a solution in groovy that gives the answers you want, you should be able to switch it to whatever language you're using:
def distributedValues(min, max, wanted) {
   def incrementBy = (max - min)/(wanted + 1)
   (1..wanted).collect { count -> min + (count * incrementBy) }
}

assert distributedValues(0, 100, 1) == [50]
assert distributedValues(0, 100, 3) == [25, 50, 75]
assert distributedValues(0, 100, 4) == [20, 40, 60, 80]
assert distributedValues(0, 100, 5) == [16.6666666667, 33.3333333334, 50.0000000001, 66.6666666668, 83.3333333335]
assert distributedValues(100, 200, 3) == [125, 150, 175]

