Skip to content

Instantly share code, notes, and snippets.

@arcthur
Created October 19, 2015 07:13
Show Gist options
  • Save arcthur/eb9625407b37c70d1353 to your computer and use it in GitHub Desktop.
Save arcthur/eb9625407b37c70d1353 to your computer and use it in GitHub Desktop.
d3-random using ramdajs
import R from 'ramda';
const rRandom = (x = Math.random() * 2 - 1, y = Math.random() * 2 - 1) => {
const r = x * x + y * y;
if (!r || r > 1) return rRandom();
else return { x: x, r: r };
}
const normal = (µ = 0, σ = 1) => {
const rr = rRandom();
return µ + σ * rr.x * Math.sqrt(-2 * Math.log(rr.r) / rr.r);
};
const logNormal = R.compose(Math.exp, normal);
const irwinHall = R.compose(
R.reduce((a, b) => (a + b), 0),
R.times(() => Math.random())
);
const bates = m => irwinHall(m) / m;
export default {
normal: R.curry(normal),
logNormal: R.curry(logNormal),
irwinHall: R.curry(irwinHall),
bates: R.curry(bates)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment