Created
October 19, 2015 07:13
-
-
Save arcthur/eb9625407b37c70d1353 to your computer and use it in GitHub Desktop.
d3-random using ramdajs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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