### Highlights from Random Points in an n-Dimensional Hypersphere

4.8125
4.8 | 17 ratings Rate this file 6 Downloads (last 30 days) File Size: 770 Bytes File ID: #9443 Version: 1.0

# Random Points in an n-Dimensional Hypersphere

### Roger Stafford (view profile)

24 Dec 2005 (Updated )

Randomly and uniformly distributes points throughout a hypersphere.

File Information
Description

This creates a set of random points defined by Cartesian coordinates and uniformly distributed over the interior of an n-dimensional hypersphere of radius r with center at the origin.

The 'randn' function is first used to create independent multivariate normally distributed sets of n random variables, each representing points in n-dimensional space. The incomplete gamma function, 'gammainc', is then used to map these points radially to the interior of an n-dimensional hypersphere of finite radius r in such a way as to be spatially uniformly distributed.

MATLAB release MATLAB 5.2 (R10)
25 Jul 2016 P.-O.

13 Aug 2015 Job

### Job (view profile)

% Super elegant!
% For the specific 3D case, howeverm, there is a 4x faster solution:

phi = 2*pi*rand(n,1);
costheta = 1 - 2*rand(n,1);

X = [...

13 Aug 2015 Job

### Job (view profile)

18 Mar 2013 Kuan-Sung

### Kuan-Sung (view profile)

Hi,

In AMB's code, we should generate X again before we perform Laurent's approach. Then the results from 2 approaches are similar.

Thanks,
Kuan-Sung

20 Nov 2012 AMB

### AMB (view profile)

Hi - This post is in reference to Laurent's comments.

Using this sample code, we see that Laurent's code substitution is faster, but the result is different from the original. Laurent's modification appears to produce data that spans a different radius than the original and is comprised of data which when viewed on the screen in a 2D projection appears more centrally clustered. The original code produces more defined boundaries.

Both ideas may have their own virtues depending on application.

m = 30000;n = 2; r = 2;
X = randn(m,n);
s2 = sum(X.^2,2);
tic
X = X.*repmat(r*(gammainc(s2/2,n/2).^(1/n))./sqrt(s2),1,n);
toc
subplot(1,2,1);plot(X(:,1),X(:,2),'r.','markersize',1);
axis equal;zoom off; zoom on;drawnow;shg;
ax = axis;

tic
X = X.*repmat(r*(rand(m,1).^(1/n))./sqrt(s2),1,n);
toc
subplot(1,2,2);plot(X(:,1),X(:,2),'r.','markersize',1);
axis equal;zoom off; zoom on;drawnow;shg;
axis(ax);

pause

m = 30000;n = 3; r = 2;
X = randn(m,n);
s2 = sum(X.^2,2);
tic
X = X.*repmat(r*(gammainc(s2/2,n/2).^(1/n))./sqrt(s2),1,n);
toc
subplot(1,2,1);plot3(X(:,1),X(:,2),X(:,3),'r.','markersize',1);
axis equal;rotate3d off; rotate3d on;drawnow;shg;
ax = axis;

tic
X = X.*repmat(r*(rand(m,1).^(1/n))./sqrt(s2),1,n);
toc
subplot(1,2,2);plot3(X(:,1),X(:,2),X(:,3),'r.','markersize',1);
axis equal;rotate3d off; rotate3d on;drawnow;shg;
axis(ax);

pause

m = 30000;n = 20; r = 2;
X = randn(m,n);
s2 = sum(X.^2,2);
tic
X = X.*repmat(r*(gammainc(s2/2,n/2).^(1/n))./sqrt(s2),1,n);
toc
subplot(1,2,1);plot3(X(:,1),X(:,2),X(:,3),'r.','markersize',1);
axis equal;rotate3d off; rotate3d on;drawnow;shg;
ax = axis;

tic
X = X.*repmat(r*(rand(m,1).^(1/n))./sqrt(s2),1,n);
toc
subplot(1,2,2);plot3(X(:,1),X(:,2),X(:,3),'r.','markersize',1);
axis equal;rotate3d off; rotate3d on;drawnow;shg;
axis(ax);

28 Apr 2011 Vincent Dubourg

### Vincent Dubourg (view profile)

Hi again,

I googled a little more and finally found a short explanation in the book by Rubinstein (2008, p. 69). I leave the link here in case it deserves someone else...

Cheers,
Vincent

Comment only
28 Apr 2011 Vincent Dubourg

### Vincent Dubourg (view profile)

Hi!

Thank you for sharing this neat function! I was wondering if you could give me any reference explaining the theoretical concepts underlying this script?

Thanks,
Vincent

Comment only
11 Feb 2011 Laurent

### Laurent (view profile)

The convenient use of the incomplete gamma function luckily provided by Matlab can be avoided, so as to make the function probably faster, more elegant and reliable. No non-elementary function is necessary here.

In the last line, simply replace
gammainc(s2/2,n/2)
by
rand(m,1)

This amount to sampling the radius randomly, independently of the point "X/sqrt(s2)" that is uniform on the surface of the sphere. Indeed, one can check that if u is uniform on [0,1] then r*u^(1/n) has same distribution as the norm of a vector chosen uniformly inside the ball B(0,r) in n-dimensional space.

I don't have matlab right here to check for speed compared to using gammainc, but in any case this method does not rely on some blackbox approximate integration, which makes it, to my taste, more elegant. It is also more portable into other programming languages.

10 Aug 2010 chen ?

### chen ? (view profile)

Thanks a lot!
I have another question.If I wanna to get a uniform distribution on the surface of the hypersphere (radius equals 1), whether is it correct to normalize the generated vectors to be length 1. Thank you very much!

01 Jul 2010 Jiaqi

### Jiaqi (view profile)

05 Apr 2010 Florian

### Florian (view profile)

The elegancy is breathtaking! I wish you would post more functions!

15 Jan 2010 molc morloc

### molc morloc (view profile)

31 Mar 2009 MathMoose

### MathMoose (view profile)

24 Sep 2007 Aree Witoelar

Thanks a lot for the elegant code! I had trouble generating this distribution. Before this, my solution was to sample and reject; but for high dimensions it was too crude and consuming.

03 Apr 2007 Steve Lord

Very nice piece of code. I have just two small suggestions for enhancements to the M-file help:

1) Include the calling syntax early in the help. I will admit I sometimes forget the calling syntax for functions I don't use often; my first resort is to type "help <functionname>", either to refresh my memory or to copy and paste the syntax into my code. In a case like this, I can see users perhaps forgetting the order of the parameters.

2) Include an example. Having a example that users can copy and paste included in the help gives people who may not know how to use your function a "known good" state -- from there, they can tweak. For a straightforward function like this, it may not seem necessary, but I think it's a good habit. It could be as simple as the two lines of code to generate the sample image.

23 Jun 2006 Lee Jones

elegant procedure, surprisingly

23 Jan 2006 Peter Nave

The compactness of the code is stunning.

29 Dec 2005 John D'Errico

I'll admit it took me a minute or so to verify this code. Its
mathematically elegant. Its efficient. It even works in the
special case of n = 1.

28 Dec 2005 Peter (PB) Bodin

Simple, yet very clever solution.