Sure. In NR they propose to do pretty much what you are doing, but they made one step further to avoid sin/cos calls. Here is the code from NR:
if (iset == 0) { //We don't have an extra deviate handy, so do { v1=2.0*ran1(idum)-1.0; //pick two uniform numbers in the square extending //from -1 to +1 in each direction, v2=2.0*ran1(idum)-1.0; rsq=v1*v1+v2*v2; // see if they are in the unit circle, } while (rsq >= 1.0 || rsq == 0.0); //and if they are not, try again. fac=sqrt(-2.0*log(rsq)/rsq);
//Now make the Box-Muller transformation to get two normal deviates. Return one and //save the other for next time. gset=v1*fac; iset=1; //Set flag. return v2*fac; } else { //We have an extra deviate handy, iset=0; //so unset the flag, return gset; //and return it. }
As I mentioned before switching to this schema improved performance of my app by 20%. App is doing monte-carlo simulation, it spends majority of the time generating normally distributed random numbers, but does some other calculation as well, so performance improvement is even bigger.
-----Original Message----- Can you elaborate as to what the difference between Box-Muller and the implemented method is? As far as I understand Box-Muller it is just the implemented algorithm.
|