Skip to content

Instantly share code, notes, and snippets.

@StanczakDominik
Created December 18, 2020 15:51
Show Gist options
  • Save StanczakDominik/70795e7ba5e2ad57f205c5131061ebde to your computer and use it in GitHub Desktop.
Save StanczakDominik/70795e7ba5e2ad57f205c5131061ebde to your computer and use it in GitHub Desktop.
Sampling a point on a unit sphere
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Three ways of sampling a random point on a unit sphere's surface"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.141592653589793"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import random\n",
"import math\n",
"π = math.pi"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The incorrect (biased one) one;"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"823 ns ± 16.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"θ = 2 * π * random.random()\n",
"φ = π * random.random()\n",
"\n",
"x = math.sin(θ) * math.cos(φ)\n",
"y = math.sin(θ) * math.sin(φ)\n",
"z = math.cos(θ)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The Fermat Library one;\n",
"[Source](https://twitter.com/fermatslibrary/status/1339922144080367619)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"862 ns ± 13.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"u = random.random()\n",
"v = random.random()\n",
"θ = 2 * π * u\n",
"φ = math.acos(2*v-1)\n",
"\n",
"sinθ = math.sin(θ)\n",
"x = sinθ * math.cos(φ)\n",
"y = sinθ * math.sin(φ)\n",
"z = math.cos(θ)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The one by @norpadon\n",
"[Source](https://twitter.com/norpadon/status/1339923332796432388)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.72 µs ± 158 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"x = random.normalvariate(0, 1)\n",
"y = random.normalvariate(0, 1)\n",
"z = random.normalvariate(0, 1)\n",
"\n",
"r = math.sqrt(x**2 + y**2 + z **2)\n",
"x /= r\n",
"y /= r\n",
"z /= r"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# And with NumPy:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10000"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"N = 10000"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The Fermat Library implementation:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.32 ms ± 14 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"u = np.random.random(N)\n",
"v = np.random.random(N)\n",
"θ = 2 * π * u\n",
"φ = np.arccos(2*v-1)\n",
"\n",
"sinθ = np.sin(θ)\n",
"x = sinθ * np.cos(φ)\n",
"y = sinθ * np.sin(φ)\n",
"z = np.cos(θ)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.06 ms ± 13 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"x = np.random.normal(size=N)\n",
"y = np.random.normal(size=N)\n",
"z = np.random.normal(size=N)\n",
"\n",
"r = np.sqrt(x**2 + y**2 + z **2)\n",
"x /= r\n",
"y /= r\n",
"z /= r"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment