MATLAB: How to generate random uniform disturbed random vectors of euclidian lenght of one

uniform distributionvector

Hi MATLAB Community,
I am trying to randomly generate uniformly distributed vectors, which are of Euclidian length of 1. By uniformly distributed I mean that each entry (coordinate) of the vectors is uniformly distributed.
More specifically, I would like to create a set of, say, 1000 vectors (lets call them V_i, with i=1,…,1000), where each of these random vectors has unit Euclidian length and the same dimension V_i=(v_1i,…,v_ni)' (let’s say n = 5, but the algorithm should work with any dimension). If we then look on the distribution of e.g. v_1i, the first element of each V_i, then I would like that this is uniformly distributed.
In the attached MATLAB example you see that you cannot simply draw random vectors from a uniform distribution and then normalize the vectors to Euclidian length of 1, as the distribution of the elements across the vectors is then no longer uniform.
Is there a way to generate this set of vectors such, that the distribution of the single elements across the vector-set is uniform?
Thank you for any ideas.
PS: MATLAB is our Language of choice, but solutions in any languages are, of course, welcome.
clear all
rng('default')
nvar=5;
sample = 1000;
x = zeros(nvar,sample);
for ii = 1:sample
y=rand(nvar,1);
x(:,ii) = y./norm(y);
end
hist(x(1,:))
figure
hist(x(2,:))
figure
hist(x(3,:))
figure
hist(x(4,:))
figure
hist(x(5,:))

Best Answer

My conjecture: the only dimension that is possible of generate uniform distribution for all components is n==3 (n is nvars in the question). In that case the solution is
m = 1000000;
n = 3; % only possible dimension
x = randn(m,n); % Important: Do not change Normmal law to something else
x = x ./ sqrt(sum(x.^2,2));
Check
figure
for k=1:n
subplot(1,n,k);
hist(x(:,k),100);
xlabel(sprintf('x_%d',k));
ylabel('count');
end
Related Question