QGpCoreTools/Random.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of QGpCoreTools.
00004 **
00005 **  This library is free software; you can redistribute it and/or
00006 **  modify it under the terms of the GNU Lesser General Public
00007 **  License as published by the Free Software Foundation; either
00008 **  version 2.1 of the License, or (at your option) any later version.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00013 **  License for more details.
00014 **
00015 **  You should have received a copy of the GNU Lesser General Public
00016 **  License along with this library; if not, write to the Free Software
00017 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 **
00019 **  See http://www.geopsy.org for more information.
00020 **
00021 **  Created : 2002-10-13
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (ULg, Liège, Belgium)
00025 **    Marc Wathelet (LGIT, Grenoble, France)
00026 **
00027 ***************************************************************************/
00028 
00029 #ifndef RANDOM_H
00030 #define RANDOM_H
00031 
00032 #include <math.h>
00033 
00034 #include "GaussDistribution.h"
00035 #include "Translations.h"
00036 #include "Trace.h"
00037 
00038 namespace QGpCoreTools {
00039 
00040 #define RANDOM_NTAB 32
00041 #define RANDOM_STATE_SIZE (RANDOM_NTAB+3)*sizeof(qint64)
00042 
00043 class QGPCORETOOLS_EXPORT Random
00044 {
00045   TRANSLATIONS("Random")
00046 public:
00047   Random(int seed=0);
00048   Random(const QByteArray& s);
00049 
00050   inline int uniform(int min, int max);
00051   inline double uniform(double min, double max);
00052   double normal(double mean, double stddev);
00053   double normal(GaussDistribution & gd) {return normal(gd.mean(),gd.stddev());}
00054   double ran2();
00055 
00056   QByteArray state() const;
00057 
00058   void testPeriod();
00059   void testDistribution(double max, qint64 nReset);
00060 private:
00061   //int _nCalls;
00062   qint64 _idum;
00063   qint64 _idum2;
00064   qint64 _iy;
00065   qint64 _iv[RANDOM_NTAB];
00066 };
00067 
00068 inline double Random::uniform(double min, double max)
00069 {
00070   return min+(max-min)*ran2();
00071 }
00072 
00073 inline int Random::uniform(int min, int max)
00074 {
00075   return min+(int)round((max-min)*ran2());
00076 }
00077 
00078 } // namespace QGpCoreTools
00079 
00080 #endif // RANDOM_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines