Advertisement
Nam_Hoang_Waw

GetOneGaussianByBoxMuller

Mar 2nd, 2019
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.41 KB | None | 0 0
  1. #include "Random.h"
  2. #include <cstdlib>
  3. #include <cmath>
  4.  
  5. double GetOneGaussianByBoxMuller()
  6. {
  7.     double result;
  8.  
  9.     double x;
  10.     double y;
  11.  
  12.     double sizeSquared;
  13.     do
  14.     {
  15.         x = 2.0*rand()/static_cast<double>(RAND_MAX)-1;
  16.         y = 2.0*rand()/static_cast<double>(RAND_MAX)-1;
  17.         sizeSquared = x*x + y*y;
  18.     }
  19.     while
  20.         ( sizeSquared >= 1.0);
  21.  
  22.     result = x*sqrt(-2*log(sizeSquared)/sizeSquared);
  23.  
  24.     return result;
  25.  
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement