Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

/magnus/back_end/general/include/RandomNumbers.h

Go to the documentation of this file.
00001 /*
00002  *   $Id: RandomNumbers.h,v 1.4 1999/11/23 20:37:35 bormotov Exp $
00003  */
00004 
00005 // Copyright (C) 1995 The New York Group Theory Cooperative
00006 // See magnus/doc/COPYRIGHT for the full notice.
00007 
00008 // Contents: Definitions of classes UniformRandom and NormalRandom,
00009 //           for generating random numbers.
00010 //
00011 // Principal Author: Roger Needham
00012 //
00013 // Status: Useable.
00014 //
00015 // Revision History:
00016 //
00017 // * 07/96 Aleksey M. implemented IPC tools.
00018 //
00019 // * 09/97 Dmitry B. added function long TimeSeed( ).
00020 //         From now on UniformRandom and NormalRandom use it as a
00021 //         default seed value. 
00022 //
00023 
00024 #ifndef _RANDOMNUMBERS_H_
00025 #define _RANDOMNUMBERS_H_
00026 
00027 #include <iostream.h>
00028 #include "IPC.h"
00029 #include "Timer.h"
00030 
00031 extern "C" double sqrt(double);
00032 extern "C" double log(double);
00033 
00034 
00035 inline long TimeSeed() {
00036   timeval tp;
00037   gettimeofday(&tp,0);
00038   return tp.tv_sec;
00039 }
00040 
00041 
00042 class UniformRandom {
00043 
00044 // An object in this class generates uniformly random numbers of type
00045 // int and float.
00046 
00047 public:
00048 
00049   UniformRandom(int seed = TimeSeed()) { reseed(seed); }
00050 
00051   void reseed(int seed);
00052   // In particular, calling this with the same seed as passed to the
00053   // constructor starts the random sequence over.
00054 
00055   float rand( );
00056   // Returns uniformly random floats in [0.0, 1.0].
00057 
00058   int rand(int lower, int upper);
00059   // Returns uniformly random ints in [lower, upper]
00060 
00061 
00062   /////////////////////////////////////////////////////////////////////////
00063   //                                                                     //
00064   // IPC tools:                                                          //
00065   //                                                                     //
00066   /////////////////////////////////////////////////////////////////////////
00067 
00068   friend ostream& operator < ( ostream& ostr, const UniformRandom& unRand )
00069   {
00070     ostr < unRand.ix1 < unRand.ix2 < unRand.ix3;
00071     for (int i = 0; i<98; i++) { ostr << unRand.r[i] << ' ';}// There is no > and < 
00072     return ostr;                                             // operators for float in IPC.h
00073   }
00074   
00075   friend istream& operator > ( istream& istr, UniformRandom& unRand)
00076   {
00077     istr > unRand.ix1 > unRand.ix1 > unRand.ix2 > unRand.ix3;
00078     for (int i = 0; i<98; i++) { istr >> unRand.r[i];}
00079      return istr;
00080   }
00081 
00082 private:
00083 
00084   static const int   M1  = 259200;
00085   static const int   IA1 = 7141;
00086   static const int   IC1 = 54773;
00087   static const float RM1 = (1.0/M1);
00088   static const int   M2  = 134456;
00089   static const int   IA2 = 8121;
00090   static const int   IC2 = 28411;
00091   static const float RM2 = (1.0/M2);
00092   static const int   M3  = 243000;
00093   static const int   IA3 = 4561;
00094   static const int   IC3 = 51349;
00095 
00096   long ix1, ix2, ix3;
00097   float r[98];
00098 };
00099 
00100 
00101 
00102 class NormalRandom {
00103 
00104 // An object in this class generates normally distributed random numbers
00105 // of type int and float.
00106 
00107 public:
00108 
00109   NormalRandom(int seed = TimeSeed()) : uniformDeviates(seed), iset(0) { }
00110 
00111   void reseed(int seed) { uniformDeviates.reseed(seed); }
00112   // In particular, calling this with the same seed as passed to the
00113   // constructor starts the random sequence over.
00114 
00115   float rand( );
00116   // Returns normally distributed random floats with mean 0 and variance 1.
00117 
00118   int rand(int mean, int stddev);
00119   // Returns normally distributed random ints with mean mean and standard
00120   // deviation stddev. For example, rand(100, 20) returns ints, roughly 68%
00121   // of which fall within 20 of 100, and 95% within 40 of 100.
00122   // Beware that rand(100, 20) could return a negative int, though it is
00123   // unlikely. You may want something like
00124   //   do { i = G.rand(100, 20); } while ( i < 1 );
00125 
00126   /////////////////////////////////////////////////////////////////////////
00127   //                                                                     //
00128   // IPC tools:                                                          //
00129   //                                                                     //
00130   /////////////////////////////////////////////////////////////////////////
00131 
00132   friend ostream& operator < ( ostream& ostr, const NormalRandom& noRand )
00133   {
00134     ostr < noRand.uniformDeviates < noRand.iset;
00135     ostr << noRand.gset << ' ';
00136     return ostr;
00137   }
00138   
00139   friend istream& operator > ( istream& istr, NormalRandom& noRand)
00140   {
00141     istr > noRand.uniformDeviates > noRand.iset;
00142     istr >> noRand.gset;
00143     return istr;
00144   }
00145 
00146 private:
00147 
00148   UniformRandom uniformDeviates;
00149   int iset;
00150   float gset;
00151 };
00152 
00153 #endif

Generated at Tue Jun 19 09:49:35 2001 for Magnus Classes by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001