Skip to content

Commit

Permalink
Actually use seedrandom.
Browse files Browse the repository at this point in the history
The tests are run in a sandboxed environment, and so didn’t have access to the
same global Math whose random was being overridden.
  • Loading branch information
mbostock committed Jan 10, 2014
1 parent aee1214 commit 8f919c4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
5 changes: 5 additions & 0 deletions test/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ module.exports = function() {
return topic;
};

topic.sandbox = function(_) {
sandbox = _;
return topic;
};

topic.document = function(_) {
var document = jsdom.jsdom("<html><head></head><body></body></html>");

Expand Down
10 changes: 5 additions & 5 deletions test/math/random-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var suite = vows.describe("d3.random");

suite.addBatch({
"random": {
topic: load("math/random").expression("d3.random"),
topic: load("math/random").sandbox({Math: Math}).expression("d3.random"),
"(using seedrandom)": {
topic: function(random) {
_random = Math.random;
Expand Down Expand Up @@ -75,14 +75,14 @@ function KSTest(cdf, n) {
// Derivation of this interval is difficult.
// @see K-S test in Knuth's AoCP vol.2
assert.inDelta(K_positive, 0.723255, 0.794145);
}
};
}

// Logistic approximation to normal CDF around N(mean, stddev).
function normalCDF(mean, stddev) {
return function(x) {
return 1 / (1 + Math.exp(-0.07056 * Math.pow((x-mean)/stddev, 3) - 1.5976 * (x-mean)/stddev));
}
};
}

// See http://en.wikipedia.org/wiki/Log-normal_distribution#Similar_distributions
Expand All @@ -91,7 +91,7 @@ function logNormalCDF(mean, stddev) {
var numerator = Math.exp(mean);
return function(x) {
return 1 / (Math.pow(numerator / x, exponent) + 1);
}
};
}

function irwinHallCDF(n) {
Expand All @@ -111,7 +111,7 @@ function irwinHallCDF(n) {
t += Math.pow(-1, k % 2) * binoms[k] * Math.pow(x - k, n);
}
return t / normalisingFactor;
}
};
}

function factorial(n) {
Expand Down

0 comments on commit 8f919c4

Please sign in to comment.