Published
Edited
Mar 20, 2021
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
// Pick a random point uniformly on the cylinder and project
// horizontally onto the sphere. Needs 2 uniform variates.
// Proof this preserves area: Archimedes (225 BCE)
// http://en.wikipedia.org/wiki/On_the_Sphere_and_Cylinder
cylinder_random = () => {
const
a = 2*Math.PI*random(),
u = random(),
s = Math.sqrt(u/(1 - u));
return [Math.cos(a)*s, Math.sin(a)*s];
}
Insert cell
Insert cell
// Marsaglia (1972) http://doi.org/10.1214/aoms/1177692644
// Take a random point in a unit-radius disk and project to
// the 2-sphere via a (2d) radially symmetric function.
// Needs ~2.5 uniform variates.
ball2_rejection_random = () => {
let x, y, r2; do {
x = random() - 0.5;
y = random() - 0.5;
r2 = x*x + y*y;
} while (r2 >= 0.25);
const q = Math.sqrt(0.25 - r2);
return [x/q, y/q];
}
Insert cell
Insert cell
// Normalize magnitude of a point in the unit-radius ball.
// Needs ~5.7 uniform variates.
ball3_rejection_random = () => {
let x, y, z, r; do {
x = random() - 0.5;
y = random() - 0.5;
z = random() - 0.5;
r = Math.hypot(x, y, z);
} while (r >= 0.25);
const q = z + r;
if (q === 0) return [Infinity, 0];
return [x/q, y/q];
}
Insert cell
Insert cell
// Cook (1957) https://doi.org/10.1090/S0025-5718-1957-0690630-7
// This needs ~13.0 uniform variates per point, but has the
// advantage of requiring no square roots or circular functions,
// only rational arithmetic.
ball4_rejection_random = () => {
let a, b, c, d; do {
a = random() - 0.5;
b = random() - 0.5;
c = random() - 0.5;
d = random() - 0.5;
} while (a*a + b*b + c*c + d*d >= 0.25);
const q = a*a + b*b;
if (q === 0) return [Infinity, 0];
return [(a*c + b*d)/q, (a*d - b*c)/q];
}
Insert cell
Insert cell
// Normalize 3-tuple of gaussian variates.
gauss3_random = () => {
const
x = randn(),
y = randn(),
z = randn(),
q = z + Math.hypot(x, y, z);
if (q === 0) return [Infinity, 0];
return [x/q, y/q];
}
Insert cell
Insert cell
// Like `ball4_rejection_random` but using gaussian variates.
// Any radially symmetric distribution in 4-space would work.
gauss4_random = () => {
const
a = randn(),
b = randn(),
c = randn(),
d = randn(),
q = a*a + b*b;
if (q === 0) return [Infinity, 0];
return [(a*c + b*d)/q, (a*d - b*c)/q];
}
Insert cell
// Inverse project stereographic to geographic coordinates.
lonlat = ([x, y]) => [
180/Math.PI * Math.atan2(y, x),
90 - 360/Math.PI * Math.atan(Math.hypot(x, y))
]
Insert cell
// Any infinite coordinate represents the south pole.
lonlat([Infinity, 0])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more