3

I am writing a JavaScript application to generate random points on the surface of a sphere. I found this formula on Wolfram to get phi and theta (http://mathworld.wolfram.com/SpherePointPicking.html#eqn2). Phi is the one I am having issues with phi = cos^(-1)(2v-1) where v is a random variables of (0,1). The following is the JavaScript I wrote to calculate phi. However, when it executes it returns NaN very often. Can anyone tell me if I am misinterpreting the formula? The only thing I can think of is Math.random() generates (0,1] and the 0 can throw an error. But in running multiple tests, I don't think Math.random() is generating a 0 every time I execute the line.

var phi = Math.acos(2 * (Math.random()) * - 1);

Mehrdad
  • 708
  • 1
  • 17
  • 38
robertlara
  • 60
  • 2
  • 8

2 Answers2

3

It's because the arc cosine of a number greater than 1 (the Math.acos() function) returns NaN, and Math.random() * 2 sometimes returns numbers greater than 1.

How to fix?

I see the maths cos^(-1)(2v-1)

as something like

v = someValue;

Math.acos(2 * v) - 1;

To do this with you want, you probably want

phi = Math.acos(2 * Math.random() - 1);

as your Javascript code.

Conclusion:

To fix, all you need to do is replace

phi = Math.acos(2 * Math.random() * - 1);

with

phi = Math.acos(2 * Math.random() - 1);

(remove the * -1)

Cilan
  • 13,101
  • 3
  • 34
  • 51
  • Not sure why downvoted. The observation in the answer does explain why the OP is getting `NaN` values (+1 to offset the downvote). – Rob W Dec 26 '13 at 23:40
  • @RobW It's because I posted a wrong formula in the original edit, I'm creating a new one. Also, thanks for the **+1** :) – Cilan Dec 26 '13 at 23:42
  • Well, even if that's true, then the downvoter could add a comment to explain what's wrong. It doesn't take a minute, and helps everyone: you will know how to correct your answer, and future readers who are puzzled about the downvote will know what's wrong with a seemingly plausible answer. – Rob W Dec 26 '13 at 23:44
  • Not sure where you're getting the 1/Math.acos from. The OP's formula says cos^-1 which is just Math.acos (arccosine). The Math.floor is also incorrect. – Arnavion Dec 27 '13 at 00:03
  • @Arnavion I edited my post, it's just that I got confused because when I first put that, someone downvoted my answer... I thought I got my maths wrong. Well, I guess since he can't un-downvote I just have to *wait for people to up-vote it* ;) – Cilan Dec 27 '13 at 00:23
  • Ok, I see it now Man of Snow. Thanks for the great explanation, I tried to upvote your response, but it says I dont have enough Stackoverflow street cred yet. Thank you so much for the well explained response. – robertlara Dec 27 '13 at 02:06
  • @gr4yf0x You're close, you only need 15 REP, but when you do get enough REP, you can upvote ;) You can also re-pay me by accepting my answer, though! Just click the blank check mark. – Cilan Dec 27 '13 at 02:53
  • @gr4yf0x So if I didn't make it clear, to accept my answer, press the blank check mark outlined in black. See [this image.](http://i.stack.imgur.com/uqJeW.png). And then later, or right now, quickly by editing some post and waiting for your edit to get approved, you will have enough REP to upvote my answer, if you want. – Cilan Dec 27 '13 at 02:58
0

You have an extra multiplication.

var phi = Math.acos(2 * (Math.random()) * - 1);

should be

var phi = Math.acos(2 * Math.random() - 1);
Arnavion
  • 3,627
  • 1
  • 24
  • 31