MathOverflow is a question and answer site for professional mathematicians. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


I need to generate a 'uniform sample' over an hemisphere and once done project it in a specific vector direction.

I have try the following, but it produce some errors... maybe you have an idea ? I have another solution, but I don't understand why this one does not work !

1. Create a random direction

x = random() * 2 - 1

y = random() * 2 - 1

z = random() * 2 - 1

d = normalize(x,y,z) // Scale it to have vector lenght = 1

2. Project the sample into the direction

dp = dot_product(d, direction)

if dp <= 0 then result = -d

else result = d


share|cite|improve this question

closed as off topic by Dan Petersen, Ricky Demer, Henry Cohn, Andy Putman, Kate Juschenko Jul 18 '12 at 19:36

Questions on MathOverflow are expected to relate to research level mathematics within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

Your random vector $(x,y,z)$ is not distributed correctly. – Dan Petersen Jul 17 '12 at 9:10

This is fully discussed in

Efficiently sampling points uniformly from the surface of an n-sphere

share|cite|improve this answer
Thanks, I have read it both references. I already use one of the solution in the Wolfram reference (this one works of course). But, the solution that does not work for me is the last of from the Wolfram link, formula (16). So, it should work ! – spectral Jul 17 '12 at 9:39
I use simple random numbers that I scale between [-1,1] like this : (rnd*2-1) – spectral Jul 17 '12 at 9:40
In the last variant in the Wolfram link you're supposed to use normally distributed random variables, yours are not. In any case, your questions are off topic on this site, which is about research level mathematics question. In the future you would be better off asking at – Dan Petersen Jul 17 '12 at 9:55
Thanks, Why my random values are not "normally distributed" ? – spectral Jul 17 '12 at 10:02
For a reason that won't be given here, since the question is off topic. $\:$ (See Dan's comment.) – Ricky Demer Jul 17 '12 at 10:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.