CoCoALib-0.9905 date: 23 May 2007


random.H

Go to the documentation of this file.
00001 #ifndef CoCoA_random_H
00002 #define CoCoA_random_H
00003 
00004 //   Copyright (c)  2006  John Abbott
00005 
00006 //   This file is part of the source of CoCoALib, the CoCoA Library.
00007 
00008 //   CoCoALib is free software; you can redistribute it and/or modify
00009 //   it under the terms of the GNU General Public License (version 2)
00010 //   as published by the Free Software Foundation.  A copy of the full
00011 //   licence may be found in the file COPYING in this directory.
00012 
00013 //   CoCoALib 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 CoCoA; if not, write to the Free Software
00020 //   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 
00022 
00023 #include <cstddef>
00024 //using std::size_t;
00025 #include <cstdlib>
00026 //using std::initstate;
00027 #include <iosfwd>
00028 //using std::ostream;
00029 #include <vector>
00030 //using std::vector;
00031 
00032 namespace CoCoA
00033 {
00034 
00035   class RandomBitStream
00036   {
00037   public:
00038     explicit RandomBitStream(unsigned long seed=1);
00039     // Default copy ctor and assignment are OK.
00040     // Default dtor is OK.
00041     bool operator*() const;
00042     RandomBitStream& operator++();
00043     RandomBitStream operator++(int);
00044     std::size_t myIndex() const;
00045     void myOutputSelf(std::ostream& out) const;
00046   private:
00047     static const std::size_t ourStateBytes = 64; // read the Unix/Linux manual before changing this value!
00048     void myFillBuffer(); // fills myBuffer and resets myBitMask
00049   private: // data members
00050     long myBitMask;
00051     long myBuffer;
00052     std::size_t myCounter;
00053     const unsigned long mySeed;
00054     std::vector<char> myState;
00055   };
00056 
00057   bool sample(RandomBitStream& RBS);
00058   bool prob(double P, RandomBitStream& RBS); // returns true with probability P
00059   std::ostream& operator<<(std::ostream& out, const RandomBitStream& RBS);
00060 
00061   //---------------------------------------------------------------------------
00062   // Inline functions.
00063 
00064   inline bool RandomBitStream::operator*() const
00065   {
00066     return myBitMask&myBuffer;
00067   }
00068 
00069 
00070   inline RandomBitStream& RandomBitStream::operator++()
00071   {
00072     ++myCounter;
00073     myBitMask <<= 1;
00074     if (myBitMask <= 0) myFillBuffer();
00075     return *this;
00076   }
00077 
00078 
00079   inline bool sample(RandomBitStream& RBS)
00080   {
00081     return *++RBS; // advance then get value
00082   }
00083 
00084 } // end of namespace CoCoA
00085 
00086 
00087 
00088 // RCS header/log in the next few lines
00089 // $Header: /Volumes/Home/cocoa/cvs-repository/CoCoALib-0.99/include/CoCoA/random.H,v 1.1.1.1 2007/03/09 15:16:11 abbott Exp $
00090 // $Log: random.H,v $
00091 // Revision 1.1.1.1  2007/03/09 15:16:11  abbott
00092 // Imported files
00093 //
00094 // Revision 1.5  2006/12/06 17:18:40  cocoa
00095 // -- removed #include "config.H"
00096 //
00097 // Revision 1.4  2006/11/27 14:19:30  cocoa
00098 // -- added #include <cstddef> //using std::size_t;
00099 //
00100 // Revision 1.3  2006/10/27 19:01:17  cocoa
00101 // Corrected a spelling mistake: Steam has become Stream :-)
00102 //
00103 // Revision 1.2  2006/10/06 14:04:15  cocoa
00104 // Corrected position of #ifndef in header files.
00105 // Separated CoCoA_ASSERT into assert.H from config.H;
00106 // many minor consequential changes (have to #include assert.H).
00107 // A little tidying of #include directives (esp. in Max's code).
00108 //
00109 // Revision 1.1.1.1  2006/05/30 11:39:37  cocoa
00110 // Imported files
00111 //
00112 // Revision 1.2  2006/04/27 13:45:30  cocoa
00113 // Changed name of NewIdentityRingHom to NewIdentityHom.
00114 // Changed name of member functions which print out their own object
00115 // into myOutputSelf (to distinguish from "transitive" myOutput fns).
00116 //
00117 // Revision 1.1  2006/04/14 13:51:40  cocoa
00118 // Added a class for generating random bits (RandomBitStream in the file random.H).
00119 //
00120 
00121 #endif

Generated on Wed May 23 13:43:35 2007 for CoCoALib by  doxygen 1.4.6