# Uniform sampling hemisphere and project in a specific direction [closed]

Hi,

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

Thanks

-

## closed as off topic by Dan Petersen, Ricky Demer, Henry Cohn, Andy Putman, Kate JuschenkoJul 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. mathworld.wolfram.com/SpherePointPicking.html – Dan Petersen Jul 17 '12 at 9:10

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