Skip to content

Instantly share code, notes, and snippets.

@kchapelier
Last active June 25, 2017 18:19
Show Gist options
  • Save kchapelier/a6c719fb0a2b5eccb79f58e744838072 to your computer and use it in GitHub Desktop.
Save kchapelier/a6c719fb0a2b5eccb79f58e744838072 to your computer and use it in GitHub Desktop.
Uniform distribution on a sphere
//Return random directions, spherical angle pairs theta [0,PI) and phi [0,TWO_PI), distributed uniformly over a sphere
//Pairs generated with theta=random(PI) and phi=random(TWO_PI) have a higher density around the poles.
//See spherical coordinates: https://en.wikipedia.org/wiki/Spherical_coordinate_system
//Optimalization by Dan Gries
//https://pastebin.com/7uPYaA8G
function randomThetaPhi () {
"use strict";
var eps = Math.random(),
z = 1 - 2 * eps,
t = Math.PI * 2 * Math.random();
return [Math.acos(z), t];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment