gaussian_2d.h

00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006, 2007, Andrew W. Steiner
00005   
00006   This file is part of O2scl.
00007   
00008   O2scl is free software; you can redistribute it and/or modify
00009   it under the terms of the GNU General Public License as published by
00010   the Free Software Foundation; either version 3 of the License, or
00011   (at your option) any later version.
00012   
00013   O2scl is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016   GNU General Public License for more details.
00017   
00018   You should have received a copy of the GNU General Public License
00019   along with O2scl. If not, see <http://www.gnu.org/licenses/>.
00020 
00021   -------------------------------------------------------------------
00022 */
00023 #ifndef O2SCL_GAUSSIAN_2D_H
00024 #define O2SCL_GAUSSIAN_2D_H
00025 
00026 #include <o2scl/rnga.h>
00027 
00028 #ifndef DOXYGENP
00029 namespace o2scl {
00030 #endif
00031   
00032   /** 
00033       \brief Generate two random numbers from a normal distribution
00034       
00035       \todo Double check that sigma is implemented correctly
00036       
00037   */
00038   template<class rng_t> class gaussian_2d {
00039   public:
00040     
00041     rng_t r;
00042 
00043     /** \brief Generate two numbers from a 
00044         distribution with zero mean and standard deviation sigma
00045     */
00046     void random(double sigma, double &x, double &y) {
00047       
00048       double x1, x2, w, y1, y2;
00049       
00050       do {
00051         x1 = 2.0 * r.random() - 1.0;
00052         x2 = 2.0 * r.random() - 1.0;
00053         w = x1 * x1 + x2 * x2;
00054       } while ( w >= 1.0 );
00055       
00056       w = sqrt( (-2.0 * log( w ) ) / w );
00057       x = x1 * sigma * w;
00058       y = x2 * sigma * w;
00059       
00060       return;
00061     }
00062     
00063   };
00064 
00065 #ifndef DOXYGENP
00066 }
00067 #endif
00068 
00069 #endif

Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.

Project hosting provided by SourceForge.net Logo, O2scl Sourceforge Project Page