Search code examples
javascriptarraysfrequency-distribution

How do I create a distribution group of a range of numbers by a given number of groups


In Javascript, if I have a range of numbers, say 0-10000. like this:

var min = 0;
var max = 10000;

and I want to split this range into a number of buckets by a input:

var buckets = 5;

in this case, I want to return an array of numbers that splits up this range: ie. the resulting numbers would be: 0, 2000, 4000, 6000, 8000, 10000

if I said 10 buckets, the numbers would be 0, 1000, 2000, etc....

My issue is if I have 8 buckets, 12 buckets 32 buckets.... How would I do this in javascript? Thanks.


Solution

  • var dif = max - min;
    var a = dif/bucked;
    
    for (var i=min; i<=max;i=i+a){
      i;
    }