Skip to content
the poor coder

javascript random point on unit sphere

Poor Man
Published
Reading time
2 min read
On this page

JavaScript: Random Point on Unit Sphere#

Generating a random point on a unit sphere can be useful in various applications such as computer graphics, physics simulations, and game development. In this post, we will discuss a simple method to obtain a random point on a unit sphere using JavaScript.

The Algorithm#

The algorithm to generate a random point on a unit sphere is straightforward. We will use spherical coordinates (r, θ, φ) to represent the point. Here r = 1 (unit sphere), θ ∈ [0, 2π] (longitude), and φ ∈ [0, π] (latitude).

First, we generate two random numbers u and v between 0 and 1 using Math.random() function.

Text
const u = Math.random();
const v = Math.random();

Next, we calculate the longitude and latitude using the following formulas:

Text
const longitude = 2 * Math.PI * u;
const latitude = Math.acos(2 * v - 1);

Finally, we convert the spherical coordinates to Cartesian coordinates (x, y, z) using the following formulas:

Text
const x = Math.sin(latitude) * Math.cos(longitude);
const y = Math.sin(latitude) * Math.sin(longitude);
const z = Math.cos(latitude);

The resulting vector (x, y, z) is a random point on a unit sphere.

The Code#

Here's the complete code:

Text
function getRandomPointOnUnitSphere() {
  const u = Math.random();
  const v = Math.random();
  const longitude = 2 * Math.PI * u;
  const latitude = Math.acos(2 * v - 1);
  const x = Math.sin(latitude) * Math.cos(longitude);
  const y = Math.sin(latitude) * Math.sin(longitude);
  const z = Math.cos(latitude);
  return { x, y, z };
}

const randomPoint = getRandomPointOnUnitSphere();
console.log(randomPoint);

The above code defines a function getRandomPointOnUnitSphere() that returns a random point on a unit sphere as an object with properties x, y, and z. We then call this function and log the result to the console.

Alternative Methods#

There are other methods to generate random points on a unit sphere. One such method is rejection sampling. In this method, we generate random points in a cube and reject those outside the sphere until we obtain a point inside the sphere. However, this method can be less efficient than the spherical coordinates method.

Another method is to use quaternions to generate random rotations and apply them to a fixed vector. However, this method can be more complex and requires knowledge of quaternion algebra.

Overall, the spherical coordinates method is simple and efficient for generating random points on a unit sphere in JavaScript.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC