d3.random.logNormal can be optimized #787

Closed
graysonlang opened this Issue Aug 26, 2012 · 2 comments

Projects

None yet

2 participants

@graysonlang

Currently a call to d3.random.logNormal invokes an unnecessary function call and no-op math. It can be rewritten as follows:

logNormal: function(µ, σ) {
      var n = arguments.length;
      if (n < 2) σ = 1;
      if (n < 1) µ = 0;
      return function() {
        var x, y, r;
        do {
          x = Math.random() * 2 - 1;
          y = Math.random() * 2 - 1;
          r = x * x + y * y;
        } while (!r || r > 1);
        return Math.exp+ σ * x * Math.sqrt(-2 * Math.log(r) / r));
      };
    }
@mbostock
D3 member

Yes, but then you've duplicated the code from d3.random.normal.

@mbostock
D3 member

Fixed in #1020.

@mbostock mbostock closed this Jan 23, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment